This looks like a really common task, but I can't find an easy way to do it.

I want to undo the last applied migration. I would have expected a simple command, like

PM> Update-Database -TargetMigration:"-1"

Instead, all I can come up with is:

PM> Get-Migrations

Retrieving migrations that have been applied to the target database.

PM> Update-Database -TargetMigration:"CategoryIdIsLong"

(At least I can use just the name, skipping the timestamp...)

Is there an easier way?

Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233

16 Answers16


I want to add some clarification to this thread:

Update-Database -TargetMigration:"name_of_migration"

What you are doing above is saying that you want to rollback all migrations UNTIL you're left with the migration specified. Thus, if you use GET-MIGRATIONS and you find that you have A, B, C, D, and E, then using this command will rollback E and D to get you to C:

Update-Database -TargetMigration:"C"

Also, unless anyone can comment to the contrary, I noticed that you can use an ordinal value and the short -Target switch (thus, -Target is the same as -TargetMigration). If you want to rollback all migrations and start over, you can use:

Update-Database -Target:0

0, above, would rollback even the FIRST migration (this is a destructive command--be sure you know what you're doing before you use it!)--something you cannot do if you use the syntax above that requires the name of the target migration (the name of the 0th migration doesn't exist before a migration is applied!). So in that case, you have to use the 0 (ordinal) value. Likewise, if you have applied migrations A, B, C, D, and E (in that order), then the ordinal 1 should refer to A, ordinal 2 should refer to B, and so on. So to rollback to B you could use either:

Update-Database -TargetMigration:"B"


Update-Database -TargetMigration:2

Edit October 2019:

According to this related answer on a similar question, correct command is -Target for EF Core 1.1 while it is -Migration for EF Core 2.0.

  • 12,626
  • 9
  • 52
  • 59
  • 31
    The name of the migration with index 0 is `$InitialDatabase`. – MEMark Sep 09 '14 at 19:52
  • 1
    Thanks. Are there any $(name) values to refer to other index positions, such as $LatestDatabase, or something like that? – Jazimov Sep 10 '14 at 05:07
  • I don't know. I haven't been able to find any by simple googling. Maybe browsing the EF source code would reveal them? – MEMark Sep 11 '14 at 06:57
  • 3
    FYI, we just ran into this and were looking to do the same thing as OP... using `ls variable:*` it looks like `$InitialDatabase` is simply a PowerShell variable defined as 0, there aren't any other ones defined, even in the current EF source code. And, get-migrations doesn't return anything, it only writes to the console, so you can't iterate over the objects returned... – DrewJordan Oct 22 '14 at 20:03
  • How do you update the model class to reflect the revert/newly executed migration? – Don Cheadle Sep 27 '15 at 22:33
  • The short -Target switch can be further abbreviated to -t. The migrations commands are PowerShell scripts. PowerShell script switches can be shorted to the fewest number of characters necessary to still be unique – Josh Feb 23 '18 at 23:08

As of EF 5.0, the approach you describe is the preferred way. So

PM> Update-Database -TargetMigration:"NameOfSecondToLastMigration"

or using your example migrations

PM> Update-Database -TargetMigration:"CategoryIdIsLong"

One solution would be to create a wrapper PS script that automates the steps above. Additionally, feel free to create a feature request for this, or better yet, take a shot at implementing it! https://github.com/dotnet/ef6

  • 2,538
  • 1
  • 27
  • 55
Andrew Peters
  • 11,135
  • 4
  • 37
  • 34
  • 5
    Another detail: If you have an existing Manual Migration you need to roll back to, but realized your "Down" method doesn't really roll things back properly, you can just edit and save it, then rerun update-database -target... until it rolls back properly. Modifying the Manual Migration after the fact - after you've already applied it - doesn't turn it into something that's not allowed to be edited. – Chris Moschini Dec 04 '13 at 08:13
  • 2
    This does not work for me. All I get is "specified target migration "-1" does not exist. – tutiplain Aug 06 '17 at 11:51
  • 3
    @tutiplain Look at his second code block. You quoting what he wishes existed, not what does exist. – Sinjai Jun 22 '18 at 01:48
  • what is the correct way of doing this using dotnet command? Sorry i could not find in the documentation or i am missing something. Tried via https://www.entityframeworktutorial.net/efcore/cli-commands-for-ef-core-migration.aspx#migrations-remove any suggestions? – Sijan Shrestha May 23 '20 at 17:52
  • I found the solution, `dotnet ef database update LastGoodMigration`, however, this seems confusing as I am coming from a node background and using knex, sequlize, they have a command to rever the last change, is there any command to revert the last action that was applied to the database? – Sijan Shrestha May 23 '20 at 17:55
  • 3
    -TargetMigration didn't work BUT -Migration did, EF Core 5.0.7 – Aaron. S Jun 09 '21 at 23:25

In EntityFrameworkCore:

Update-Database 20161012160749_AddedOrderToCourse

where 20161012160749_AddedOrderToCourse is a name of migration you want to rollback to.

  • 3,706
  • 5
  • 32
  • 39
  • 3
    GEM! It took me a while to find this answer (as they've changed it for .NET Core). Definitely worth an upvote! – JsAndDotNet Oct 28 '16 at 13:07
  • 6
    You don't need to include the date/time. You can just put in the name. – Richard Marskell - Drackir Jan 26 '18 at 19:53
  • 1
    This does not work for me... it will say "Done" but all migrations after the one I specify still remain. And none of the "Down" code in any of those migrations has been executed. – egmfrs Mar 21 '18 at 14:21
  • one can also run back to a specific migration like that: Update-Database -Migration: "AddedOrderToCourse" Especially when using blanks in migration names, this is the way to do it. (i.e. Update-Database -Migration "Added Order to Course") – SwissCoder Jun 04 '18 at 14:35

I realised there aren't any good solutions utilizing the CLI dotnet command so here's one:

dotnet ef migrations list
dotnet ef database update NameOfYourMigration

In the place of NameOfYourMigration enter the name of the migration you want to revert to.

Then you can remove all the reverted migrations for good using

dotnet ef migrations remove
  • 934
  • 10
  • 17

The solution is:

Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess
  • 1,224
  • 2
  • 19
  • 30
  • 538
  • 1
  • 6
  • 16


PM> Update-Database yourMigrationName

(reverts the migration)

PM> Update-Database

worked for me

in this case the original question (yourMigrationName = CategoryIdIsLong)

  • 131
  • 1
  • 3
  • The previous solution doesn't work for EF Core , parameter error, whereas this two step solution worked, I was able to remove-migration after this whereas before I got an error message: The migration '20210201060139_cascadeDelete' has already been applied to the database. Revert it and try again. when remove-migration was tried.attempeted. – David Jones Feb 01 '21 at 08:49

In EF Core you can enter the command Remove-Migration in the package manager console after you've added your erroneous migration.

The console suggests you do so if your migration could involve a loss of data:

An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy. To undo this action, use Remove-Migration.

  • 1
    This only works when the migration hasn't been applied on the database yet. – Pieter Meiresone Jan 11 '21 at 20:47
  • This doesn't rollback the migration, this removes it from the migrations. If the migration is applied to the database, it doesn't change it, it just removes the migration from code. – John Demetriou Jun 02 '23 at 11:58
update-database 0

Warning: This will roll back ALL migrations in EFCore! Please use with care :)

  • 312
  • 4
  • 10

Additional reminder:

If you have multiple configuration type, you need to specify the [ConfigurationName]

Update-Database -Configurationtypename [ConfigurationName] -TargetMigration [MigrationName]
  • 2,222
  • 1
  • 26
  • 42

I run mine through my (BASH GIT) console also running Entity Framework Core. Update-Database commands will not work outside of the package console and I have to use the donet ef commands.

donet ef database update [Name of previous Migration]

This will run the protected override void Down(MigrationBuilder migrationBuilder) method of your current migration and all of the others to get back to the version of the DB you set it to.

I also use the -p [migration project] -s [Project Solution]. This also allows it to point to my appsettings.[Enviorment].json where my password to access DB is stored.

export ASPNETCORE_ENVIRONMENT=[ENVIORMENT]; donet ef database update [Name of previous Migration] -p [Migration Project Name] -s [Solution Name]

A lot of this might be known but wanted to give detail in case your first time doing.

  • 473
  • 7
  • 13


Update database to the previous point

update-database CategoryIdIsLong

And then, remove the bad migration

  • 1,461
  • 1
  • 19
  • 20

I'm using EntityFrameworkCore and I use the answer by @MaciejLisCK. If you have multiple DB contexts you will also need to specify the context by adding the context parameter e.g. :

Update-Database 201207211340509_MyMigration -context myDBcontext

(where 201207211340509_MyMigration is the migration you want to roll back to, and myDBcontext is the name of your DB context)

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
Update-Database –TargetMigration:"Your migration name"

For this problem I suggest this link:


Mojtaba Nava
  • 858
  • 7
  • 17

I found that this works when run in the Package Manager Console:

dotnet ef migrations list | select -Last 2 | select -First 1 | ForEach-Object { Update-Database -Migration $_ }

You could create a script that makes it easier.

Alex Dresko
  • 5,179
  • 3
  • 37
  • 57

In case there is a possibility for dataloss EF does not complete the update-database command since AutomaticMigrationDataLossAllowed = false by default, and roolbacks the action unless you run it with the -force parameter.

Update-Database –TargetMigration:"Your migration name" -force


Update-Database –TargetMigration:Your_Migration_Index -force
  • 508
  • 7
  • 15

This solution is effective within a project that is dependent on Entity Framework Core 7.

Remove-Migration -Force

This command will:

  • Reverts the effects of the most recently applied migration on the database without necessitating the use of the "Update-Database" command.
  • Deletes the associated migrations and snapshots.


Kindly note that executing this command will not prompt for confirmation. The most recently generated migration and its related database changes will be reversed.

If the migration being rolled back involves the removal of columns or tables, it's important to recognize that there is a possibility of data loss. Any information stored within the columns or tables affected by the removed migration will be lost without any confirmation prompt.

If your application utilizes multiple DbContexts, simply include the name of the targeted DbContext in the command:

Remove-Migration -context <SecondDbContext> -Force
Husam Ebish
  • 4,893
  • 2
  • 22
  • 38