Jakob Busk Sørensen
Jakob Busk Sørensen

Reputation: 6081

How to restore migration history with EF Core

Let's say I have a database which is created by three migrations. They can be seen in the table dbo._EFMigrationsHistory and the content could look like this:

MigrationId                         ProductVersion
20200516181941_InitialMigration     3.1.4
20200517091058_AddedIndexes         3.1.4
20200605115456_IntroducedBreweries  3.1.4

Now let's say that I for some reason lose all of the table (or just a part of it). So suddenly the table look like this:

MigrationId                         ProductVersion
20200517091058_AddedIndexes         3.1.4
20200605115456_IntroducedBreweries  3.1.4

And then I make a new migration, because i changed something in the database

Add-Migration SomethingChanged

This will work fine, since we aren't talking to the database at this point. But if I try to update the database, like this

Update-Database

It will fail, because it it trying to re-run the InitialMigration (or at least so I assume). So a common error is that the tables created in the InitialMigration cannot be created, because they already exist.

During development you can simply nuke the database and/or migrations, but that's not a very viable strategy for production. It can also be done with Script-Migration, which has a from/to option, allowing something like this:

Script-Migration 20200517091058_AddedIndexes 20200605115456_IntroducedBreweries

But this doesn't seem to work on Update-Database (not in the documentation either).

Is there any (production safe) way to fix this, so that the migration history isn't broken for all eternity?

The best way I can think of, is to manually insert the missing row(s) in the database, but this seems quite hack-ish...

Upvotes: 3

Views: 3672

Answers (1)

mathis1337
mathis1337

Reputation: 1619

The Issue: You have mucked up your migrations and you would like to reset it without deleting your existing tables.

The Problem: You can't reset migrations with existing tables in the database as EF wants to create the tables from scratch.

What to do:

  1. Delete existing migrations from Migrations_History table.
  2. Delete existing migrations from the Migrations Folder.
  3. Run add-migration Reset. This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.)
  4. You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. However, you can't apply the migration that you just made as the tables already exist in your database. So go into the Migration and comment out all the code inside the "Up" method.
  5. Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.

You have now reset your migrations and may continue with normal migrations.

Upvotes: 4

Related Questions