Entity Framework rollback and remove bad migration

后端 未结 8 2273
[愿得一人]
[愿得一人] 2020-12-12 09:01

I\'m using EF 6.0 for my project in C# with manual migrations and updates. I have about 5 migrations on the database, but I realised that the last migration was bad and I do

相关标签:
8条回答
  • 2020-12-12 09:28

    For EF 6 here's a one-liner if you're re-scaffolding a lot in development. Just update the vars and then keep using the up arrow in package manager console to rinse and repeat.

    $lastGoodTarget = "OldTargetName"; $newTarget = "NewTargetName"; Update-Database -TargetMigration "$lastGoodTarget" -Verbose; Add-Migration "$newTarget" -Verbose -Force
    

    Why is this necessary you ask? Not sure which versions of EF6 this applies but if your new migration target has already been applied then using '-Force' to re-scaffold in Add-Migration will not actually re-scaffold, but instead make a new file (this is a good thing though because you wouldn't want to lose your 'Down'). The above snippet does the 'Down' first if necessary then -Force works properly to re-scaffold.

    0 讨论(0)
  • 2020-12-12 09:34

    As the question indicates this applies to a migration in a development type environment that has not yet been released.

    This issue can be solved in these steps: restore your database to the last good migration, delete the bad migration from your Entity Framework project, generate a new migration and apply it to the database. Note: Judging from the comments these exact commands may no longer be applicable if you are using EF Core.

    Step 1: Restore to a previous migration

    If you haven't yet applied your migration you can skip this part. To restore your database schema to a previous point issue the Update-Database command with -TargetMigration option specify the last good migration. If your entity framework code resides in a different project in your solution, you may need to use the '-Project' option or switch the default project in the package manager console.

    Update-Database –TargetMigration: <name of last good migration>
    

    To get the name of the last good migration use the 'Get-Migrations' command to retrieve a list of the migration names that have been applied to your database.

    PM> Get-Migrations
    Retrieving migrations that have been applied to the target database.
    201508242303096_Bad_Migration
    201508211842590_The_Migration_applied_before_it
    201508211440252_And_another
    

    This list shows the most recent applied migrations first. Pick the migration that occurs in the list after the one you want to downgrade to, ie the one applied before the one you want to downgrade. Now issue an Update-Database.

    Update-Database –TargetMigration: "<the migration applied before it>"
    

    All migrations applied after the one specified will be down-graded in order starting with the latest migration applied first.

    Step 2: Delete your migration from the project

    remove-migration name_of_bad_migration
    

    If the remove-migration command is not available in your version of Entity Framework, delete the files of the unwanted migration your EF project 'Migrations' folder manually. At this point, you are free to create a new migration and apply it to the database.

    Step 3: Add your new migration

    add-migration my_new_migration
    

    Step 4: Apply your migration to the database

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