Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge migrations in entity-framework-core

Tags:

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 !

like image 855
Raskolnikov Avatar asked Nov 22 '16 11:11

Raskolnikov


People also ask

Does ef core support automatic migration?

Entity Framework introduced automated migration so that you don't have to process database migration manually for each change you make in your domain classes. The automated migrations can be implemented by executing the enable-migrations command in the Package Manager Console.

How do you add migration to multiple contexts?

Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.

How do I use migrations in Entity Framework?

Step 1 − Before running the application you need to enable migration. Step 2 − Open Package Manager Console from Tools → NuGet Package Manger → Package Manger Console. Step 3 − Migration is already enabled, now add migration in your application by executing the following command.


1 Answers

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

like image 98
Sampath Avatar answered Dec 17 '22 08:12

Sampath