Merge migrations in entity-framework-core

One way to do that is removing all migration files physically and adding new one. If your migrations are in "Migrations" folder, you can simply delete it, otherwise you need to delete your "ModelSnapshot" file too. I think this approach can solve your issue.


When you want to merge not all but N last migrations, the protocol is not the same:

  1. Revert N last migrations, one by one, each with the 2 following commands:
    • dotnet ef database update NameOfTheLastMigration
    • dotnet ef migrations remove
  2. Apply reverts to database:
    • dotnet ef database update
  3. Create the "merge" migration:
    • dotnet ef migrations add NameOfTheMergeMigration

EF 6.X has a option IgnoreChanges. That is the perfect fit for your scenario. But unfortunately it is not a feature available in EF core.

But there is a workaround.

Step 1 : Delete all the migration scripts in the Migrations folder.

Step 2 : In the package manager console : run

PM> Add-Migration InitialCreate

Step 3 : Delete both Up() and Down() methods code. Before you do this, keep those methods saved elsewhere as we will need them again in step 5.

Step 4 : run:

 PM> Update-Database

It'll insert a new record into __EFMigrationsHistory table.

Step 5 : After that fill the above migration script's (i.e. .._InitialCreate) Up() and Down() method from the content kept in a safe place from Step 3.

That is it. Now you have only 1 migration file :)

Note : EF core with Package manager console (PM) :Package Manager Console