0

I'm new to .net and I accidentally deleted my table for my database. Is there any way to recover it, or will I have to redo it, and if so, how do I redo it was built using Code-First. Here is my table data.

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace ArtistDatabase.Models
{
public class Artist
{
    public int ID { get; set; }
    //----------------------------------------------------------------------------------------------
    [StringLength(60, MinimumLength = 3)]
    public string Name { get; set; }
    //----------------------------------------------------------------------------------------------
    [DataType(DataType.ImageUrl)]
    public string Picture { get; set; }
    //----------------------------------------------------------------------------------------------
    [Display(Name = "Date of Birth"),DataType(DataType.Date),DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime BirthDate { get; set; }
    //----------------------------------------------------------------------------------------------
    [Required]
    [StringLength(30)]
    public string Nationality { get; set; }
    //----------------------------------------------------------------------------------------------
    [Display(Name = "Art Style/Movement")]
    public string ArtStyle { get; set; }
    //----------------------------------------------------------------------------------------------
    public string Info { get; set; }
    //----------------------------------------------------------------------------------------------
    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
    [StringLength(5)]
    public string Rating { get; set; }
    //----------------------------------------------------------------------------------------------
    [Display(Name = "Famous work: "),DataType(DataType.ImageUrl)]
    public string Artwork1 { get; set; }
    //----------------------------------------------------------------------------------------------
    [Display(Name = " "), DataType(DataType.ImageUrl)]
    public string Artwork2 { get; set; }
    //----------------------------------------------------------------------------------------------
    [Display(Name = " "), DataType(DataType.ImageUrl)]
    public string Artwork3 { get; set; }
}

public class ArtistDBContext : DbContext
{
    public DbSet<Artist> Artists { get; set; }
}
}
Wiliam Cardoso
  • 434
  • 1
  • 8
  • 23
  • Possible duplicate of [Add new table to an existing database using Code First approach in EF 6 and MVC 5](http://stackoverflow.com/questions/27958793/add-new-table-to-an-existing-database-using-code-first-approach-in-ef-6-and-mvc) – Pavel Kovalev Sep 23 '16 at 03:40

2 Answers2

0

In VS go to Tools>NuGet Package manager>Package Manager Console

write -> Enable-Migrations and than -> Update-Database -verbose

If you gotta a error try to delete all the tables from your database, dont forget the MigrationHistory and than do the steps above again.

0

If you do not have PowerShell Package Manager Console with the version of visual studio you are using, you may follow the following link

[http://docs.nuget.org/ndocs/tools/powershell-reference][1]

Following commands are to be used with PowerShell Console

Enable-Migration command have to be executed first to enable the migration process. This step creates a configuration class file.

Enable-Migrations -ContextTypeName ArtistDBContext -ConnectionString "data source={Your source};initial catalog={dbname};integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient"

Add-Migration command is the next step to take the snapshot of the changes that are to be pushed. Initial Parameter is random, you can have anything. You can modify the contents of the class file created during this step to control the changes that are pushed to Database

Add-Migration Initial

Update-Database is the final step which pushes the changes to database. -Verbose is an optional parameter to see the progress of SQL statements on the command line

Update-Database -ConnectionString -ConnectionString "data source={Your source};initial catalog={dbname};integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose