Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 771
nam Avatar asked Jul 04 '16 21:07

nam


People also ask

How do I revert my ef migration?

Reverting a Migration But, for some reason, you want to revert the database to the previous state. In this case, use the update-database <migration name> command to revert the database to the specified previous migration snapshot. > dotnet ef database update MyFirstMigration.


2 Answers

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

like image 143
adem caglin Avatar answered Sep 19 '22 13:09

adem caglin


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

dotnet ef database update 0 dotnet ef migrations remove 
like image 25
Ronald Ramos Avatar answered Sep 21 '22 13:09

Ronald Ramos