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

313,998

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

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

Share:
313,998
nam
Author by

nam

Updated on July 08, 2022

Comments

  • nam
    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
    nam almost 8 years
    I'm still getting the same error. I first used dotnet ef database update MyFirstMigration --context BloggingContext that worked successfully. Then I ran dotnet ef migrations remove --context BloggingContext that gave me the same error message as in my post
  • bvpb
    bvpb almost 8 years
    You'll need to update to the migration before MyFirstMigration. If that is the 1st migration (as the name implies) then you can use dotnet ef database update 0 to revert(unapply) all migrations from the database. You should then be able to run dotnet ef migrations remove.
  • nam
    nam over 7 years
    But 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
    kimbaudi over 7 years
    It's good to know dotnet ef database update 0, but running dotnet ef migrations remove afterwards will remove default migration for Identity, which might not be desired.
  • kimbaudi
    kimbaudi over 7 years
    You can call Update-Database from Package Management Console or call dotnet ef database update from the command prompt from the project directory.
  • Tobias J
    Tobias J about 7 years
    Thanks for the tip on dotnet ef database update 0! I hadn't seen this mentioned anywhere...
  • Mark Amery
    Mark Amery about 7 years
    Just 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
    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
    Structed over 6 years
    Also it's worth noting that you should only use the name of the migration, excluding the date-prefix
  • mark.monteiro
    mark.monteiro over 6 years
    This 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
    Michael Freidgeim about 6 years
    The question was what to do if `dotnet ef migrations remove' returns an error "The migration has already been applied to the database"
  • Michael Freidgeim
    Michael Freidgeim about 6 years
    Useful link, should be a comment of the question
  • James Morrison
    James Morrison almost 6 years
    This worked for me but I'm using EF and .NET core 2.0 with VS2017
  • Ido Ran
    Ido Ran almost 6 years
    This 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
    reZach almost 6 years
    This is what you want to use in .NET core
  • Kieren Johnstone
    Kieren Johnstone almost 6 years
    Watch out: -Migration:0 means go back to the state with no migrations. This will clear your database
  • John Nyingi
    John Nyingi almost 6 years
    Yes according to the question asked. You can increment the value depending on the migration you want to roll back to
  • Michael Kargl
    Michael Kargl over 5 years
    Thank you! This worked perfectly Fascinated that out of all the solutions no one ever mentioned the need for step 2.
  • Chamika Sandamal
    Chamika Sandamal over 5 years
    Call dotnet ef migrations remove after this
  • Ajas Aju
    Ajas Aju over 5 years
    Yeh, Nobody mentioned the step two.Thank you
  • S.Serpooshan
    S.Serpooshan over 5 years
    Your 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
    S.Serpooshan over 5 years
    not 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
    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
    Joe Irby over 5 years
    Manually 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
    Philipp Munin over 5 years
    is there any way to do it in the run-time programmaticaly (e.g. using dbContext.GetService<IMigrator>())??
  • user3413723
    user3413723 over 5 years
    The second step I think is only necessary if you call dbContext.Database.Migrate() in your startup.cs
  • Shimmy Weitzhandler
    Shimmy Weitzhandler almost 5 years
    Can I do that from the PM console?
  • jeancallisti
    jeancallisti over 4 years
    Your 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
    Ella over 4 years
    We 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
    Post Impatica about 4 years
    I keep seeing the word unapply all over these posts. It just hit me it's not a technical term, it's the word un - apply
  • UNeverNo
    UNeverNo almost 4 years
    This will lead to an inconsistent state of the database (e.g. a column is already dropped)
  • Jim G.
    Jim G. over 3 years
    @ShimmyWeitzhandler Yes. stackoverflow.com/a/55323154/109941
  • Pabli770
    Pabli770 over 3 years
    Workin in .Net Core 3.1
  • Pajri Aprilio
    Pajri Aprilio over 3 years
    I wonder what is the method "down" used for ?
  • Gert Arnold
    Gert Arnold over 3 years
    Please don't repeat answers, it's not useful. stackoverflow.com/a/55323154/861716
  • Abdelhakim
    Abdelhakim about 3 years
    I was trying to remove the very first and sole migration I made and the '0' parameter helped :)
  • Adam Cox
    Adam Cox about 3 years
    While 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 an Add-Migration be removed, or do they remain and I need to remove them manually?? Thanks!
  • Hamid Noahdi
    Hamid Noahdi about 3 years
    dotnet ef migrations remove --force worked for me in ef core
  • Wyck
    Wyck almost 3 years
    Did you really mean database remove or did you mean migrations remove?
  • Mike S.
    Mike S. almost 3 years
    In 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
    RicardoArth over 2 years
    @Pajri Aprilio When you remove a migration, the 'down' part lists the steps necessary to undo said migration.