How to unapply a migration in ASP.NET Core with EF Core
Solution 1
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
Solution 2
To completely remove all migrations and start all over again, do the following:
dotnet ef database update 0
dotnet ef migrations remove
Solution 3
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
Solution 4
You can still use the Update-Database
command.
Update-Database -Migration <migration name> -Context <context name>
However, judging by the name of your migration i'm assuming it's the first migration so that command may not work. You should be able to delete the entry from the __MigrationHistory
table in your database and then run the Remove-Migration
command again. You could also delete the migration file and just start again.
Solution 5
To revert the last applied migration you should (package manager console commands):
- Revert migration from database:
PM> Update-Database <prior-migration-name>
- Remove migration file from project (or it will be reapplied again on next step)
- Update model snapshot:
PM> Remove-Migration
UPD: The second step seems to be not required in latest versions of Visual Studio (2017).
nam
Updated on July 08, 2022Comments
-
nam almost 2 years
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.
-
nam almost 8 yearsI'm still getting the same error. I first used
dotnet ef database update MyFirstMigration --context BloggingContext
that worked successfully. Then I randotnet ef migrations remove --context BloggingContext
that gave me the same error message as in my post -
bvpb almost 8 yearsYou'll need to update to the migration before
MyFirstMigration
. If that is the 1st migration (as the name implies) then you can usedotnet ef database update 0
to revert(unapply) all migrations from the database. You should then be able to rundotnet ef migrations remove
. -
nam over 7 yearsBut you don't want to remove all migrations. For example, you want to keep the default migration VS creates for identity (User Accounts) under Data\Migrations folder.
-
kimbaudi over 7 yearsIt's good to know
dotnet ef database update 0
, but runningdotnet ef migrations remove
afterwards will remove default migration for Identity, which might not be desired. -
kimbaudi over 7 yearsYou can call
Update-Database
from Package Management Console or calldotnet ef database update
from the command prompt from the project directory. -
Tobias J about 7 yearsThanks for the tip on
dotnet ef database update 0
! I hadn't seen this mentioned anywhere... -
Mark Amery about 7 yearsJust to clarify Brad's answer here -
<migration name>
should be the name of the migration you want to go back to (i.e. probably the migration before the one you screwed up), not the name of the migration you want to undo. -
starmandeluxe over 6 years@nam "But you don't want to remove all migrations." But some of us do. Don't assume what I want :)
-
Structed over 6 yearsAlso it's worth noting that you should only use the name of the migration, excluding the date-prefix
-
mark.monteiro over 6 yearsThis will not actually un-apply the migration, just make the framework "think" it has not been applied. Your database will be in an inconsistent state if you do this.
-
Michael Freidgeim about 6 yearsThe question was what to do if `dotnet ef migrations remove' returns an error "The migration has already been applied to the database"
-
Michael Freidgeim about 6 yearsUseful link, should be a comment of the question
-
James Morrison almost 6 yearsThis worked for me but I'm using EF and .NET core 2.0 with VS2017
-
Ido Ran almost 6 yearsThis didn't work for me. The first line was ok and remove all the migrations, on the second line I still get
The migration '20180618103514_InitialMigration' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.
-
reZach almost 6 yearsThis is what you want to use in .NET core
-
Kieren Johnstone almost 6 yearsWatch out: -Migration:0 means go back to the state with no migrations. This will clear your database
-
John Nyingi almost 6 yearsYes according to the question asked. You can increment the value depending on the migration you want to roll back to
-
Michael Kargl over 5 yearsThank you! This worked perfectly Fascinated that out of all the solutions no one ever mentioned the need for step 2.
-
Chamika Sandamal over 5 yearsCall
dotnet ef migrations remove
after this -
Ajas Aju over 5 yearsYeh, Nobody mentioned the step two.Thank you
-
S.Serpooshan over 5 yearsYour second statement: "Then try to remove last migration" is not complete in this answer, please say exactly what you mean. remove the migration file? execute a command? ...
-
S.Serpooshan over 5 yearsnot sure about the second step, in my case(latest VS2017) the migration file will be deleted automatically after calling
Remove-Migration
without problem. I'm not sure what you said "it will be reapplied again on next step"! -
MetalMikester over 5 years@S.Serpooshan Indeed. I manually deleted it but when I ran Remove-Migration it complained about my previous migration having been applied to the database. But, it works.
-
Joe Irby over 5 yearsManually editing the data in your __EFMigrationsHistory is terrible advice. Don't do it. That table is only meant to be used under-the-hood by the migration tools. If you start manually editing the data, a single mistake can create all kinds of crazy unexpected behavior in your project. It's a much better idea to use one of the other recommended solutions.
-
Philipp Munin over 5 yearsis there any way to do it in the run-time programmaticaly (e.g. using
dbContext.GetService<IMigrator>()
)?? -
user3413723 over 5 yearsThe second step I think is only necessary if you call
dbContext.Database.Migrate()
in your startup.cs -
Shimmy Weitzhandler almost 5 yearsCan I do that from the PM console?
-
jeancallisti over 4 yearsYour mistake is to try to delete/remove the very same migration as the one to which you've upgraded your database last. You can't delete a migration that's still applied (i.e. current level of the database). You can only remove that migration after you've updated your database to the last migration BEFORE that one. Therefore, you must first update your database to <previous-migration-name> (using the command he gave you) and then when you call "dotnet ef migrations remove" it implicitly deletes the LAST one, i.e. MyFirstMigration.
-
Ella over 4 yearsWe had a situation where a junior team member got confused with switching branches, and ended up deleting the migration manually from both (although it was still in the __EFMigrationsHistory table since it was already applied), so we lost the "down." This is what we ended up having to do, plus some manual reversals in SSMS. So don't do it - unless you have no other choice at this point. At least all he did was add a single new field.
-
Post Impatica about 4 yearsI keep seeing the word
unapply
all over these posts. It just hit me it's not a technical term, it's the wordun - apply
-
UNeverNo almost 4 yearsThis will lead to an inconsistent state of the database (e.g. a column is already dropped)
-
Jim G. over 3 years@ShimmyWeitzhandler Yes. stackoverflow.com/a/55323154/109941
-
Pabli770 over 3 yearsWorkin in .Net Core 3.1
-
Pajri Aprilio over 3 yearsI wonder what is the method "down" used for ?
-
Gert Arnold over 3 yearsPlease don't repeat answers, it's not useful. stackoverflow.com/a/55323154/861716
-
Abdelhakim about 3 yearsI was trying to remove the very first and sole migration I made and the '0' parameter helped :)
-
Adam Cox about 3 yearsWhile viewing folder named
Migrations
in my Visual Studio IDE, and running above commands in my Package Manager Console, it would be super useful if this answer would state the expected results. For example, does removing effect the contents of the Migrations folder? That is, should the files resulting from anAdd-Migration
be removed, or do they remain and I need to remove them manually?? Thanks! -
Hamid Noahdi about 3 yearsdotnet ef migrations remove --force worked for me in ef core
-
Wyck almost 3 yearsDid you really mean
database remove
or did you meanmigrations remove
? -
Mike S. almost 3 yearsIn your last note, I think you wanted to say: "if the destination migration you want to have after un-appling a migration, does not contain..." or "if the migration you want to un-apply to, does not contain"
-
RicardoArth over 2 years@Pajri Aprilio When you remove a migration, the 'down' part lists the steps necessary to undo said migration.