简体   繁体   中英

EF Core 3.1: Deleted all Migrations and the Database, how to recreate all tables in a new database?

I deleted all migrations in the migrations folder and also deleted the database in SQL Server. I know - now - these shouldn't be done, but I need to re-create the database structure to keep the application. I tried 'add-migration initial' but the it simply generates an empty migration file and an snapshot file. If I try the 'remove-migration' then it complains about some missing migration file.

Is there anyway to 'reset' the snapshot like I just had created the DBContext, so it will create all the tables as in the current DbSet definitions?

I think the following solutions can help you:

First of all, make sure that the database is completely deleted because you said that the database was deleted in SQL. (After you create the first migration, a table called __EFMigrationsHistory is created, where the migration records are stored, if the database is not completely deleted, you must delete this table).

Second, delete the Migrations folder completely.

In the third step, run the following command.

Enable-Migrations -EnableAutomaticMigrations -Force

In the last step, run the following command

Add-Migration Initial

Steps :

  1. Delete the state: Delete the migrations folder in your project

  2. Delete the __MigrationHistory table in your database (may be under system tables)

Run the following command in the Package Manager Console:

Enable-Migrations -EnableAutomaticMigrations -Force

Use with or without -EnableAutomaticMigrations

And finally, you can run:

Add-Migration Initial

Also this two link can help you :

Reset Entity-Framework Migrations

https://weblog.west-wind.com/posts/2016/jan/13/resetting-entity-framework-migrations-to-a-clean-slate

It seems the problem was in my project! As @CodingMytra pointed in the comments, there were an active reference to the old database in my project. For some reason it was not rebuilding and not updating, thus not reflecting the changes in the models/dbcontext. I created a new, empty project, imported all source files, generated a 'Initial' migration and updated the dabase successfully.

Thanks for all replys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM