How to unapply a migration in ASP.NET Core with EF Core

前端 未结 16 2151
余生分开走
余生分开走 2020-12-07 08:10

When I run PM> Remove-Migration -context BloggingContext in VS2015 with an ASP.NET Core project using EF Core I get the following error:

Syst         


        
相关标签:
16条回答
  • 2020-12-07 08:41

    To unapply a specific migration(s):

    dotnet ef database update LastGoodMigrationName
    or
    PM> Update-Database -Migration LastGoodMigrationName
    

    To unapply all migrations:

    dotnet ef database update 0
    or
    PM> Update-Database -Migration 0
    

    To remove last migration:

    dotnet ef migrations remove
    or
    PM> Remove-Migration
    

    To remove all migrations:

    just remove Migrations folder.

    To remove last few migrations (not all):

    There is no a command to remove a bunch of migrations and we can't just remove these few migrations and their *.designer.cs files since we need to keep the snapshot file in the consistent state. We need to remove migrations one by one (see To remove last migration above).

    To unapply and remove last migration:

    dotnet ef migrations remove --force
    or
    PM> Remove-Migration -Force
    
    0 讨论(0)
  • 2020-12-07 08:41
    1. Copy the code from the body of previous successful migration's down() method.
    2. Add a new migration using Add-Migration "migration-name"
    3. Paste the copied code from the down() method of previous migration to the new migration's Up() method:
      Up(){ //paste here }
    4. Run Update-Database
    5. Done, your changes from the previous migration should now have been reverted!
    0 讨论(0)
  • 2020-12-07 08:43

    To "unapply" the most (recent?) migration after it has already been applied to the database:

    1. Open the SQL Server Object Explorer (View -> "SQL Server Object Explorer")
    2. Navigate to the database that is linked to your project by expanding the small triangles to the side.
    3. Expand "Tables"
    4. Find the table named "dbo._EFMigrationsHistory".
    5. Right click on it and select "View Data" to see the table entries in Visual Studio.
    6. Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted).
    7. Run "dotnet ef migrations remove" again in the command window in the directory that has the project.json file. Alternatively, run "Remove-Migration" command in the package manager console.

    Hope this helps and is applicable to any migration in the project... I tested this out only to the most recent migration...

    Happy coding!

    0 讨论(0)
  • 2020-12-07 08:44

    Use:

    CLI

    > dotnet ef database update <previous-migration-name>

    Package Manager Console

    PM> Update-Database <previous-migration-name>

    Example:

    PM> Update-Database MyInitialMigration

    Then try to remove last migration.

    Removing migration without database update doesn't work because you applied changes to database.

    If using PMC, Try: PM> update-database 0 This will wipe the database and allow you to remove the Migration Snapshot on your Solution

    0 讨论(0)
  • 2020-12-07 08:44

    To revert all the migrations which are applied to DB simply run:

    update-database 0
    

    It should be followed with running Remove-Migration as many times as there are migration files visible in the Migration directory. The command deletes the latest migration and also updates the snapshot.

    0 讨论(0)
  • 2020-12-07 08:46

    To revert the last applied migration you should (package manager console commands):

    1. Revert migration from database: PM> Update-Database <prior-migration-name>
    2. Remove migration file from project (or it will be reapplied again on next step)
    3. Update model snapshot: PM> Remove-Migration

    UPD: The second step seems to be not required in latest versions of Visual Studio (2017).

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