Make first code EF4.3 First migrants ignore pending migrations

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

However, some code-based migrations exist in the Migrations folder. They will run in our development and mid-tier environments so that these databases match the code. However, I do not need to apply them locally since I created the database using the current code.

Now I need to make changes to the model and create the appropriate 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 cannot specify the Add-Migration tool in another environment because it does not guarantee that the version matches what I have locally. I need a migration that matches only the changes I made.

I seem to have a few options, but none of them are perfect:

  • Delete other migrations from the Migrations folder, run the Add-Migration command, update the database and restore the old migrations. It's simple, but it seems a little hacky.
  • Go back to the version of the model in the original control that the first migration applies to, then create it and use it to create the database. Then get the latest version, apply all the migrations, then I’m ready to add my migration. It seems like a lot of effort!
  • Create a manual migration.

Does anyone have any suggestions for managing this?

+6
code-first-migrations
source share
5 answers

What I found works very simply: do not use DbContext.Database.Create() after you DbContext.Database.Create() migrations. If you want to programmatically create a new database, use the migration API instead.

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

Then you have a complete migration history and adding additional migrations works as expected.

+2
source share

We plan to use your option number 1 ...

Our standard working procedure is to create an SQL script for each migration (using the - script option for the update database) so that the SQL scripts are applied to the final "production" InstallShield databases (we plan to use the 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.

Therefore, instead of deleting migrations from the Migrations folder (as you suggested in # 1), we use SQL Mgmt Studio to manually apply only parts of the .sql files that insert inserts into _MigrationHistory.

This updates the _MigrationHistory of the local database with the changes that are already included in this database.

But it is kludge, and we are still looking for the best solution.

Dadcat

+2
source share

I ran into the same problem. If you run

 Update-database 

and then run

 Add-Migration YourMigrationName 

This solves the problem.

+1
source share

You either need to run β€œ update-database ” from the package manager console to make changes to the database, or you can delete the pending migration file ([201203271113060_AddTableX]) from the Migrations folder and then run β€œadd-migration” again to create a new one migration based on your changes.

+1
source share

just exclude the old migration file from the solution files.

0
source share

All Articles