Raskolnikov Raskolnikov - 15 days ago 9
C# Question

Merge migrations in entity-framework-core

Is it possible to merge all migrations files into one ?

I created initial migration.

dotnet ef migrations add InitialMigration


Source

When ever I have some model change I create new migration update.

But now I have too many migration update files.
Is ti possible to merge all migration files to one ?

Off course drop database is not an option, I have to preserve data !

Answer

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

But you have a workaround.

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

Step 2 : On package manager console : run

PM> Add-Migration InitialCreate

Step 3 : Delete both Up() and Down() methods code.Before do that keep those methods content separately for future use.

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 which you have keep on the safe place on Step 3.

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

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