grokky
grokky

Reputation: 9295

How do I consolidate migrations using EF Core?

I have these migrations:

00000000000000_First
20161101000000_Foo
20161102000000_Bar
// ...many many MANY more
20161108000000_Baz

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?

Upvotes: 5

Views: 3188

Answers (2)

bricelam
bricelam

Reputation: 30425

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.

Upvotes: 7

Dmitry
Dmitry

Reputation: 16825

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).

Upvotes: 2

Related Questions