Entity Framework Code Migrations - Stuck on Initial Migration

后端 未结 7 1058
面向向阳花
面向向阳花 2020-12-14 07:58

I have added EF 5 to my project via Nuget and enabled migrations with the \"Enable-Migrations\" command. I have then called \"Add-Migration\" to generate the base code for g

7条回答
  •  囚心锁ツ
    2020-12-14 08:54

    I had the same problem enabling EF migrations for a code-first model with an existing database, and the following procedure worked:

    1. Remove the existing Migrations folder in your project, and DROP the table __MigrationHistory from the existing database.
    2. Run the enable-migrations command from the Package Manager Console.
    3. Run the add-migration command to create an initial migration.
    4. Remove all of the code in Up() method for the initial migration.
    5. Run the update-database command to apply the initial migration to your database. This doesn't make any changes to existing objects (because the Up() method contains no code), but it marks the existing database as having been migrated to the initial state.
    6. Make changes to your code-first model.
    7. Run the add-migration command to create a new migration. The code in the Up() method of the new migration will contain only the changes to your object model.
    8. Run the update-database command to apply the changes to your database.

提交回复
热议问题