How to recreate my EF CodeFirst Table?
Solution 1
In system tables there is a table called
__MigrationHistory
...I deleted it.
In Package manager console ran command "update-database" Problem fixed!
More info:
Solution 2
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):
Update-database -TargetMigration:0
Normally your DB is empty now since the down methods were executed.
Update-database
This will recreate your DB to your current migration
Pinch
I'm a C# Developer in the NYC area. I just started blogging about parenting and coding. I'd appreciate a comment! pinchcode.com
Updated on August 05, 2022Comments
-
Pinch almost 2 years
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.
-
iiminov about 9 yearsSeems to work only if you have migration information defined in your project
No migrations configuration type was found in the assembly '{project_name}'.
. But it was worth a try. -
iiminov about 9 yearshandy link to the topic at hand - msdn.microsoft.com/en-us/data/jj591621.aspx
-
jk7 almost 9 yearsThe downside of this solution is that you lose your migration history.
-
Guerrilla almost 8 yearsi tried this but then it wouldn't update because the other tables already exist