Generate missing migrations so I can update an old

2019-09-12 01:36发布

问题:

I am using EF Code first with Migrations. I need to update an old database however I have lost some of the migration code. Is there a way to generate the missing migrations?

回答1:

Step 1: Back up ( obviously )

Step 2: Delete __MigrationHistory

Step 3: Create a temporary project. Use the Entity Data Model Wizard to create the Code-First Model.

Step 4: Enable migrations in the new project ( using Enable-Migrations) at the Package Manager Console.
Create an initial migration ( using add-migration one). Comment out the Up and Down methods of the created migration.
Run update-database.

Step 5: Replace all the files in the Migrations folder of the original project with the files in the temporary project. Editing DbContext and namespace names as required.

Step 6 In the original project in package manager run Add-Migration two then run update-database