Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to restore databases migration

I have a mysql database and I can not delete it. I lost my migrations in asp.net project and I changed some things in dbcontext files.

When I try make a new migration and update-database it says Table ‘aspnetroles’ already exists. What should I do? How can restore migration or how can ı equate the migration and database?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

EF Core Migrations’ dotnet ef database update will try to update the database to the newest migration. It will query the __EFMigrationsHistory table to see which migrations are applied there and which ones still need applying, if any.

The resolution for some problems is to fake having applied an update by inserting the migration history manually. This prevents having to drop a database, which of course might be problematic on production.

Insert the records as they are applied to your development database, like:

MigrationId ProductVersion
20230516084711_Initial 7.0.5

Where 20230516084711_Initial is the name of the migration file minus the .cs part (or the value in the [Migration("20230516084711_Initial")] attribute in the .Designer.cs), and 7.0.5 is an arbitrary version number of the EF Tools used to originally apply the migration.

Repeat this insertion for every migration that you need to fake being applied. Then your database update will report that it has nothing to do.

This of course will cause problems if one of your migrations which you didn’t really apply had changes with respect to the database. Make sure you only do this to migrations that definitely don’t have changes, meaning: which have definitely been integrally applied.


This can also be helpful to "squash" many migrations. If you ensure that your development database (and context and configuration) are up-to-date and consistent with the production database, you can simply delete all (or all intermediate) migrations, create a new "Initial" (or single "Feature") migration, and insert the abovementioned record on production after removing the entries belonging to the removed migrations from the migration history table.

Do note this can prove to be problematic if you have multiple developers with their own development databases; just as with pushing a rebased/squashed global Git history, everyone needs to apply the steps to synchronize their local copy with the new reality.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading