Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 297
grokky Avatar asked Nov 08 '16 10:11

grokky


People also ask

What is an Entity Framework migration bundle?

The migration bundle is a self-contained executable with everything needed to run a migration. It accepts the connection string as a parameter. It is intended to be an artifact used in continouous deployment that works with all the major tools (Docker, SSH, PowerShell, etc.).

How does EF core apply migrations?

Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.

How do you Unapply a migration in asp net core with EF core?

Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted). Run "dotnet ef migrations remove" again in the command window in the directory that has the project. json file. Alternatively, run "Remove-Migration" command in the package manager console.


1 Answers

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.

like image 122
bricelam Avatar answered Sep 29 '22 01:09

bricelam