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

前端 未结 1 1202
有刺的猬
有刺的猬 2020-12-30 02:41

We\'re using Entity Framework 5.0 Code First and Automatic Migrations.

I had a class like so:

public class TraversalZones
{
    public int Low { get;         


        
相关标签:
1条回答
  • 2020-12-30 03:19

    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.

    0 讨论(0)
提交回复
热议问题