Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recreate my EF CodeFirst Table?

I created a Code First class

A database was created in Sql automatically.

I deleted the tables :(

I have migrations enabled:

AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
Database.SetInitializer(new DropCreateDatabaseAlways<SurveyContext>());

Yet upon

update-database -force

Cannot find the object "dbo.CustomerResponses" because it does not exist or you do not have permissions.

Please help.

like image 780
Pinch Avatar asked Jan 30 '13 16:01

Pinch


People also ask

How do you refresh a table in Entity Framework?

Use the Refresh method: context. Refresh(RefreshMode. StoreWins, yourEntity);


2 Answers

In system tables there is a table called

__MigrationHistory

...I deleted it.

In Package manager console ran command "update-database" Problem fixed!

More info:

http://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system-table/

like image 129
2 revs Avatar answered Oct 06 '22 02:10

2 revs


Daf De Giraf has a better answer here that doesn't wipe out your migration history.

If you worked the correct way to create your migrations by using the command Add-Migration "Name_Of_Migration" then you can do the following to get a clean start (reset, with loss of data, of course):

  1. Update-database -TargetMigration:0
    Normally your DB is empty now since the down methods were executed.

  2. Update-database This will recreate your DB to your current migration

like image 31
jk7 Avatar answered Oct 06 '22 00:10

jk7