Merge migrations in entity-framework-core

asp.net-core c# entity-framework entity-framework-core

Question

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 !

Accepted Answer

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


Popular Answer

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.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow