Error when running Update-Database with EF 4.3

倾然丶 夕夏残阳落幕 提交于 2019-12-06 13:56:15

When you ran Enable-Migrations, the scripts may have not created an initial migration, especially if your model was mismatched to the database, or if your DbContext class is defined in a different project.

I'm not sure what the "right way" to add migrations to an existing pre-4.3 database is, but the easiest way is to dump your database. Drop the tables in it...

Make sure the Configuration file in the Migrations folder has the right context type, and then Add-Migration Initial. A big class will get built, representing your current model.

Now Update-Database will run without complaining.


If you have data that you care about in the db, you may be able to cheat and add the __MigrationHistory table that the migrations process creates to a previous backup of your database. Delete the EdmMetadata and migrations shouldn't be any wiser.

Hopefully someone who actually knows how migrations were intended to be added to an existing EF Code First database will have smoother upgrade steps?

I just ran into this on a database I "thought" was already 4.3 but was not....

I found the answer with this blog post.

Here's the basic steps.

  1. Don't make any changes to the model, really, don't!
  2. Add an initial migration.
  3. Update the database (all of them).

Now you can go about your normal business.

Here's the nitty gritty:

Don't change the model.

If you have, you need to back those out. Ugggg. I just commented out my changes. Luckily, for me, the changes were still fairly small. Of course you are doing things in small chunks anyway right. :-)

Add an initial migration.

Add-Migration "InitialModel" -IgnoreChanges

In the up script add the following:

public override void Up()    
{        
  Sql("DROP TABLE EdmMetadata");    
}

Adding the drop table is not necessary but 4.3 and on don't use EdmMetadata so, might as well.

Update the Database (all of them)

Update-Database

Do you have other servers for this database? Testing, Staging, Production? Be sure to do this final step for all of them as well. You can wait until you are done with all of your migration work before doing this to the other servers as well.

Now, continue on as normal. Make your changes and follow the normal Add-Migration & Update-Database steps for migrations.

Thanks for the question and answers. I've done the following (a composition of advises above).

How to migrate from pre-EF 4.3 with data and Code First model:

  1. Comment out all changes done comparing to current data (I needed to remove table adding).
  2. Backup database
  3. Remove table EdmMetadata
  4. Add table __MigrationHistory with the script above
  5. Run Add-Migration "InitialModel"
  6. Drop all tables except the __MigrationHistory table
  7. Run Update-database
  8. Generate script for __MigrationHistory with the newly added data. Add there Drop table EdmMetadata.
  9. Restore database and run this script.
  10. Get back the changes in the code.
  11. Run Add-Migration YOURNAMEFORNEWCHANGES
  12. Run Update-Database

And I have the old database with the data updated to EF 6. Hope that helps!

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