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

2019-03-07 21:26发布

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

System.InvalidOperationException: The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.    at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force) 
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0() 
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 
 The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.

How can I unapply it? I'm using latest release of ASP.NET Core 1.0, EF Core, and VS2015 Update 3.

11条回答
够拽才男人
2楼-- · 2019-03-07 21:59

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).

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-07 22:03

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!

查看更多
地球回转人心会变
4楼-- · 2019-03-07 22:03

In order to unapply a migration in EF Core 1.0 use the command:

dotnet ef database update {migration_name}

Use the migration name of the migration until which you would like to preserve your changes. The list of names of the migration can be found using:

dotnet ef migrations list

查看更多
成全新的幸福
5楼-- · 2019-03-07 22:11

In general if you are using the Package Manager Console the right way to remove a specific Migration is by referencing the name of the migration

Update-Database -Migration {Name of Migration} -Context {context}

Another way to remove the last migration you have applied according to the docs is by using the command:

dotnet ef migrations remove

This command should be executed from the developer command prompt (how to open command prompt) inside your solution directory.

For example if your application is inside name "Application" and is in the folder c:\Projects. Then your path should be:

C:\Projects\Application
查看更多
Bombasti
6楼-- · 2019-03-07 22:14

To completely remove all migrations and start all over again, do the following:

dotnet ef database update 0
dotnet ef migrations remove
查看更多
The star\"
7楼-- · 2019-03-07 22:14

You Should Delete migration '20160703192724_MyFirstMigration' Record From '_EFMigrationsHistory' Table.

Otherwise This Command will Remove Migration and Delete Migrations Folder:

   > remove-migration -force
查看更多
登录 后发表回答