stuck in EF migration limbo

后端 未结 7 1533
慢半拍i
慢半拍i 2020-12-20 10:58

i have somehow gotten my EF5 project into a state where I can\'t proceed.

When I do an \'update-database\' i get:

Unable to update database to

7条回答
  •  长情又很酷
    2020-12-20 11:40

    I changed the following value in my Configuration.cs class from false to true.

    AutomaticMigrationsEnabled = true; 
    

    In App_Data folder I renamed my project's database - the file with .mdf ending (so a new one will be created), and in Package Manager Console I entered the following command:

    update-database

    after which the pending migrations ran smoothly.

    Note this worked for me, but I'm not 100% if this is the best practice. In any case this Entity Framework Code First guide for migrations says:

    "If you get an error that indicates a table already exists and can't be created, it is probably because you ran the application after you deleted the database and before you executed update-database. In that case, delete the Movies.mdf file again and retry the update-database command. If you still get an error, delete the migrations folder.."

    Also about AutomaticMigrationsEnabled = true; this MSDN article, Data Points : A Code First Migrations Mystery: Solved tells "Migrations can run automatically, meaning that model changes will be discovered and migrations corresponding to changes will be created and executed on the database. All of this happens at run time during database initialization. Automatic migrations are handy for simple apps, but you have very little control over them and I typically don’t recommend enabling them. I was happy when Code First switched the default to false."

提交回复
热议问题