Make EF4.3 Code First Migrations ignore pending migrations

北战南征 提交于 2019-12-04 07:18:00

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

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.

I have encountered the same problem. If you run

Update-database

and then run

Add-Migration YourMigrationName

This solves the problem

You either need to run "update-database" from the package manager console to push your changes to the database OR you can delete the pending migration file ([201203271113060_AddTableX]) from your Migrations folder and then re-run "add-migration" to create a brand new migration based off of your edits.

simply exclude the old migration file from the solution files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!