0

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?

  • you with a `code-first` approach? – Maruf Jul 11 '22 at 05:08
  • if it has generated an empty migration file, it means somewhere some reference of your old database is still there. are you sure that you have deleted your database. try giving different name for db in connection string and do add-migration – CodingMytra Jul 11 '22 at 05:14
  • @CodingMytra It appears you´re right. I couldn't find where exactly is the error, but It seems my project wasn't building properly. I created a new empty project and imported all sources, then the add-migration/update-database worked perfectly! – Rafael Leonardi Jul 13 '22 at 19:30

2 Answers2

0

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

  • Thanks for the reply, but I already saw those articles and tried those solutions with no success. It seems the problem was in my project, for some reason, not re-building after changes and so not updating the changes to the models/dbcontext. – Rafael Leonardi Jul 13 '22 at 19:33
0

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!