EF5 Code First Migrations: “Column names in each table must be unique” error after using RenameColumn

人走茶凉 提交于 2019-11-30 08:39:10

The workaround, obviously, is this:

update-database -f -script

Which you can see the results of in my question. Then I tossed everything from the script but the last line, and ran that against the DB to let Migrations know: We already renamed that column, cut it out.

I can now proceed with this copy of the database, but I'm concerned every migration against copies of Production (until Production itself has been migrated) will keep having this issue. How can I resolve this properly without this workaround?

Update

This was in fact an issue in every other instance including Production. The dirty solution was to generate a SQL script (update-database -f -script), after committing the generated version and the fixed version.

A slightly cleaner solution is to take the SQL from the script, add a manual migration, and change the contents of Up to simply:

public void Up()
{
    Sql("...That SQL you extracted from the script...");
}

This will ensure other environments running this migration do so precisely the way you intended.

Testing this is a bit tricky so you can approach it this way:

  1. Backup your db just in case.
  2. Run the SQL. If it works properly, set the SQL aside.
  3. Add the manual migration and wipe out everything in the Up() method. Leave it completely empty.
  4. Run update-database -f
  5. Now modify the Up() method by adding the Sql("..."); calling the SQL you set aside.

Now your db is up to date without running the SQL twice, and other environments get the results of that SQL.

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