How do I consolidate migrations using EF Core?

ef-migrations entity-framework entity-framework-core


I have these migrations:

// ...many many MANY more

I want to consolidate from Foo to Baz into one new migration. I know how to do that in EF, but not in EF Core.

I tried this:

dotnet ef update database 00000000000000_First
// delete all other migration classes
dotnet ef migrations add Consolidated
dotnet ef update

I expect that the new Consolidated migration will contain all changes to the model. But its Up() and Down() methods are empty.

What am I doing wrong?

11/8/2016 10:02:18 AM

Accepted Answer

You're missing one step. Because of the new model snapshot, after you manually delete the migration files, you'll need to run dotnet ef migrations remove (or Remove-Migration in PMC) to get it back in sync.

Instead of manually deleting the files, you could also just run dotnet ef migrations remove multiple times, but it's smart enough to detect manually deleted migrations.

11/8/2016 10:31:10 PM

Popular Answer

EF Core, when executing ef migrations add, does NOT look into real database. Only your code (and DB model snapshot in Migrations folder) is used.

So, you need remove migrations from your migrations folder (using dotnet ef migrations remove multiple times) and then create consolidated using ef migrations add.

About your real database - if you want to roll it back too, you should run ef database update XXX before removing migrations. Or, if you are collapsing all migrations - you can simple drop database completely and then recreate from new migration(s).

Related Questions


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