[entity-framework] EF Migrations: Rollback last applied migration?

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.
201208012131302_Add-SystemCategory
201207311827468_CategoryIdIsLong
201207232247409_AutomaticMigration
201207211340509_AutomaticMigration
201207200025294_InitialCreate

PM> Update-Database -TargetMigration:"CategoryIdIsLong"

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

Is there an easier way?

The answer is


The solution is:

Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess

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.


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.


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.


In EntityFrameworkCore:

Update-Database 20161012160749_AddedOrderToCourse

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


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"

or

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.


EF CORE

PM> Update-Database yourMigrationName

(reverts the migration)

PM> Update-Database

worked for me

in this case the original question (yourMigrationName = CategoryIdIsLong)


Update-Database –TargetMigration:"Your migration name"

For this problem I suggest this link:

https://elegantcode.com/2012/04/12/entity-framework-migrations-tips/


Additional reminder:

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

Update-Database -Configurationtypename [ConfigurationName] -TargetMigration [MigrationName]

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)


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.


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

or

Update-Database –TargetMigration:Your_Migration_Index -force

update-database 0

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


Examples related to entity-framework

Entity Framework Core: A second operation started on this context before a previous operation completed EF Core add-migration Build Failed Entity Framework Core add unique constraint code-first 'No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync The instance of entity type cannot be tracked because another instance of this type with the same key is already being tracked Auto-increment on partial primary key with Entity Framework Core Working with SQL views in Entity Framework Core How can I make my string property nullable? Lazy Loading vs Eager Loading How to add/update child entities when updating a parent entity in EF

Examples related to entity-framework-4

Entity Framework Join 3 Tables Entity framework self referencing loop detected The object 'DF__*' is dependent on column '*' - Changing int to double LEFT JOIN in LINQ to entities? Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties How to include a child object's child object in Entity Framework 5 EF Migrations: Rollback last applied migration? The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported How to show Alert Message like "successfully Inserted" after inserting to DB using ASp.net MVC3

Examples related to database-migration

Rollback one specific migration in Laravel Access denied for user 'homestead'@'localhost' (using password: YES) How do I enable EF migrations for multiple contexts to separate databases? EF Migrations: Rollback last applied migration? Reset Entity-Framework Migrations How do I move a redis database from one server to another? How do I copy SQL Azure database to my local development server? How to export SQL Server database to MySQL?

Examples related to entity-framework-migrations

EF Core add-migration Build Failed How can I make my string property nullable? There is already an object named in the database Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration EF 5 Enable-Migrations : No context type was found in the assembly How to delete and recreate from scratch an existing EF Code First database Generate full SQL script from EF 5 Code First Migrations Entity Framework Migrations renaming tables and columns EF Migrations: Rollback last applied migration? Reset Entity-Framework Migrations