Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to collapse all existing Entity Framework migrations

The project I am working on is using Entity Framework 4.3 and data migrations to keep the schema up to date. Over the course of the project the migrations folder has grown and now has over 600 files. This is huge. We now have a binary which is over 12MB due to all the migration meta data.

I would like to collapse all these in to one migration and start again. My concerns are:

  1. Is this possible or will it cause problems with the migration history if I remove migrations?
  2. Are there any guides around describing how to do this?
like image 440
Keith Bloom Avatar asked Apr 01 '14 14:04

Keith Bloom


People also ask

How do I get rid of migrations in Entity Framework?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

How do I delete old EF core migrations?

Removing and Resetting Migrations Remove the _MigrationHistory table from the Database. Remove the individual migration files in your project's Migrations folder. Enable-Migrations in Package Manager Console. Add-migration Initial in PMC.

What is entity migration in Entity Framework?

Entity Framework Migration. Migration. The Migrations feature enables you to change the data model and deploy your changes to production by updating the database schema without having to drop and re-create the database. It is the recommended way to evolve your application's database schema if you are using the Code First workflow.

How to create consolidated EF migrations in efef core?

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.

What is a Delta migration in Entity Framework Core 5?

In Entity Framework Core 5 migrations we are adding a migration as a delta changes between our DbContext class and existing [DbContext name]ModelSnapshot. When generating new migration a CLI Tool will generate only the differences between those two and put them in two methods: Up and Down.

How to migrate domain classes in EF?

Now EF will automatically take care of the migration when you change the domain classes. In Code First Migrations, you need to execute the following commands in the Package Manager Console. Enable-Migrations: Enables the migration in your project.


1 Answers

First: I recommend that you keep your migrations in a separate assembly so they don't have to be published with the application. It could be a simple console app that applies the migrations or a winforms GUI that generates scripts. But there's no reason for it to be deployed with the app imo.

Second: Understanding that you'd be giving up the ability to roll back to previous versions, you could just exclude-from-project all of your prior migrations then generate a new one which should then be able to create a database reflecting your current model. That would serve as your new starting point. Remember that EF doesn't always generate code to do everything you want in a migration, so you might have some hand-written migration code in other migrations you'd need to pull in.

like image 52
JC Ford Avatar answered May 13 '23 20:05

JC Ford