Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make EF4.3 Code First Migrations ignore pending migrations

I have a local instance of a database that I recently created using DbContext.Database.Create(), so the __MigrationHistory table exists with an InitalCreate entry that matches the code at the moment.

Some code-based migrations exist in the Migrations folder, however. These will be run in our development and staging environments to bring those databases in line with the code. I don't need to apply them locally, however, since I created the database using the current code.

I now need to make a change to the model and create the corresponding migration. But when I run Add-Migration TestMigration, I get the following error

Unable to generate an explicit migration because the following explicit 
migrations are pending: 

[201203271113060_AddTableX, 
 201203290856574_AlterColumnY]

Apply the pending explicit migrations before attempting to generate 
a new explicit migration.

What should I do in this case? I can't point the Add-Migration tool at another environment because it's not guaranteed that version matches what I have locally. I want a migration that matches only the changes I've made.

It seems I have a few options but none are ideal:

  1. Delete the other migrations from the Migrations folder, run the Add-Migration command, upgrade the database, then restore the old migrations. This is simple but seems a bit hackish.
  2. Revert to the version of the model in source control that the first migration was applied to, then build this and use it to create the database. Then get the latest version, apply all the migrations, then I'm ready to add my migration. This seems like a lot of effort!
  3. Create the migration manually.

Does anyone have any suggestions about how to manage this?

like image 316
Joe Taylor Avatar asked Mar 29 '12 12:03

Joe Taylor


People also ask

How do you set Dbmigragrationsconfiguration AutomaticMigrationsEnabled to true to enable automatic migration?

Set DbMigrationsConfiguration. AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration. I tried AutomaticMigrationsEnabled = true , which worked without changing or adding anything!

How do I get rid of last migration in EF core?

To revert the last applied migration you should (package manager console commands): Revert migration from database: PM> Update-Database <prior-migration-name> Remove migration file from project (or it will be reapplied again on next step) Update model snapshot: PM> Remove-Migration.

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.


2 Answers

We are planning to use a variant of your Option #1...

Our Standard Operating Procedure is to generate a SQL script for each migration (using the -script option of update-database), in order to have SQL scripts to be applied to end-user "production" databases by InstallShield (we plan to use EF update-database only for developer databases).

Thus, we have both the Migration .cs files and the corresponding .sql files for all migrations in our Migrations folder.

So rather than deleting the migrations from the Migrations folder (as you proposed in #1), we use SQL Mgmt Studio to manually apply just the parts of the .sql files that do the inserts into _MigrationHistory.

That brings the _MigrationHistory of the local database up-to-date with the changes that are already incorporated into that database.

But it's a kludge, and we're still looking for a better solution.

DadCat

like image 155
DadCat Avatar answered Oct 05 '22 01:10

DadCat


What I've found works best is very simple: don't use DbContext.Database.Create() once you've enabled migrations. If you want to programmatically create a new database, use the migrations API instead.

var migrator = new DbMigrator(new Configuration());
migrator.Update();

Then you've got the full migration history and adding further migrations works just as expected.

like image 32
Joe Taylor Avatar answered Oct 05 '22 00:10

Joe Taylor