Guerrilla
Guerrilla

Reputation: 14906

Deleted table in database. How do I get entity framework to remake it?

I wrongly assumed that if I deleted a table in the database then EF would re-create an empty table next time I ran update-database.

Now I have a missing table so the application throws an exception any time it hits a reference to it.

Is there a way to tell EF to remake the table without completely deleting the database?

Upvotes: 1

Views: 3526

Answers (5)

byr
byr

Reputation: 74

In your SSOX, if you find your Tables folder, you will find the one called __MigrationHistory. Delete that one and run Update-Database. This should recreate the table according to the schema that you have.

Upvotes: 0

Lenny Woods
Lenny Woods

Reputation: 361

  1. In your migration snapshot comment out the code that pertains to the particular table you deleted.

  2. add-migration Fixit

  3. update-database

  4. The table will be recreated

  5. If you are using a DBintializer routine in your code then have it check for any data existing in the table - if not then repopulate your table

     if (!context.MyDBSet.Any())
     {
         var myData = new TableData[]
             {
     new TableData{ Data = null },
     new TableData{ Data = "Yes" },
     new TableData{ Data = "No" },
             };
    
         foreach (TableData d in myData)
         {
             context.MyDBSet.Add(d);
         }
         context.SaveChanges();
     }
    

Upvotes: 0

Bassam Alugili
Bassam Alugili

Reputation: 17003

1) Create the database locally in your computer from scratch

2) with SSM open the database and select the desired table

3) right mouse click on the table and select "script Table as" and then "Create To" --> this will generate the table creation as Sql string

4) Excute the generate query with in the application ExecuteSqlCommand

Example from my PC:

string toBeCreatetable = @"  
CREATE TABLE [dbo].[Customers]( 
    [CustomerId] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [nvarchar](max) NULL, 
 CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED  
( 
    [CustomerId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]"; 

// Somewhere in code before at the begin do:    
using (var ctx = new myDbContext(toBeCreatetable))
{
    int executed= myDbContext.Database.ExecuteSqlCommand()
}

This is the best way which guarantee that the migration history will be properly working with the conceptual model.

Upvotes: 0

Midhun Mundayadan
Midhun Mundayadan

Reputation: 3182

Just comment the table property from DbContext And Update.After That Un Comment the property and Update This will create a new table without deleting the entire database (This works for me While updating through package manager console)

Upvotes: 1

Steve Greene
Steve Greene

Reputation: 12314

The issue is EF has the table in it's snapshot with the last migration, so if you want EF to recreate it you could do this:

1) Temporarily remove the class by commenting out the DbSet and OnModelCreating (if exists).

2) Run a migration so EF removes the table from the snapshot. You will need to comment out the code in the Up() method that removes the table since it's already deleted.

3) Uncomment step 1 code.

4) Run another migration to add the table back.

https://msdn.microsoft.com/en-US/data/dn481501?f=255&MSPPError=-2147217396

Upvotes: 3

Related Questions