Reputation: 23868
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.
Upvotes: 485
Views: 651661
Reputation: 489
Just adding a new response when using SQL scripts.
First, get the list of migration of your project with:
dotnet ef migrations list
It will give something like that :
migration1
...
migrationN-1
migrationN
Now execute :
dotnet ef migrations script migrationN migrationN-1
It will produce a SQL script that you will be able to execute on your database that will revert your last migration.
Upvotes: 3
Reputation: 351
Simply try using -force parameter. So the final command would looks like :
Remove-Migration -force
Upvotes: 3
Reputation: 1067
All the commands will be written using dotnet
.
This solution is provided for .net Core 3.1, but should be compatible with next versions as well
cd to_your_project
then dotnet ef migrations remove
Note: Removing a migration works only, if you didn't execute yet dotnet ef database update
or called in your c# code Database.Migrate()
, in other words, only if the migration is not applied to your database yet.
dotnet ef migrations add <your_changes>
and apply it, which is recommended by microsoft.dotnet ef database update <your_migration_name_to_jump_back_to>
Note: If you choose to reverse a migration but the specific column or table involved is already present and being used in your database, this action will remove that column or table entirely. As a result, you will lose any data stored in them.
After reverting the migration, you can remove your unwanted migration
Upvotes: 23
Reputation: 3374
As the question itself deals with a first migration this answer is for anyone who is here looking for way to revert their last migration since most answers do not cover the alternate scenarios (For most of us it's not our first migration and we cannot wipe the entire migration history).
These commands are for Package Manager Console.
Let's say you have the following migrations in your code:
If you have already applied the migration to the db like in the question
Run:
Update-Database -Migration <Last_Good_Migration_Name>
Note: The name should be without date prefix. Eg: Update-Database -Migration LastGoodMigration
Then to remove the bad migration run:
Remove-Migration
Note: Since we have two bad migrations run the Remove Migration
command twice.
If you haven't applied the migration to the db
Just run:
Remove-Migration
In our case run the Remove Migration
command twice.
Hope this helps someone.
UPDATE August 2023
If you have multiple db contexts add -Context Context_Name
to each command. For example:
Remove-Migration -Context MyDbContext
Upvotes: 13
Reputation: 5364
To revert the last applied migration you should:
PM> Update-Database <prior-migration-name>
PS/cmd> dotnet ef database update <prior-migration-name>
dbContext.Database.Migrate()
/MigrateAsync()
in Main
/Startup
: remove the latest migration file from project (or it will be reapplied again on next step)PM> Remove-Migration
PS/cmd> dotnet ef migrations remove
Upvotes: 81
Reputation: 375
In order to unapply a migration in EF Core 1.0 use the command:
dotnet ef database update {migration_name}
Use the migration name of the migration until which you would like to preserve your changes. The list of names of the migration can be found using:
dotnet ef migrations list
Upvotes: 9
Reputation: 1120
Simply you can target a Migration by value
Update-Database -Migration:0
Then go ahead and remove it
Remove-Migration
NOTE this is for when you want to clear database.
Upvotes: 40
Reputation: 4974
Because I've been redirected to this question for searching about roll back migration in ef
not ef core
, I'm adding this answer to the question for everybody who want to know about the same problem in ef
. If you're using ef
you can use the following command to roll back the migration:
Update-Database [[-Migration] <String>]
[-Context <String>]
[-Project <String>]
[-StartupProject <String>]
[<CommonParameters>]
Based on question:
Update-Database -Migration <previous-migration-name> -context BloggingContext
Upvotes: 2
Reputation: 76
Removing a Migration
You can remove the last migration if it is not applied to the database. Use the following remove commands to remove the last created migration files and revert the model snapshot.
Package Manager Console
PM> remove-migration
CLI> dotnet ef migrations remove
The above commands will remove the last migration and revert the model snapshot to the previous migration. Please note that if a migration is already applied to the database, then it will throw the following exception.
The migration 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.
Reverting a Migration
Suppose you changed your domain class and created the second migration named MySecondMigration using the add-migration command and applied this migration to the database using the Update command. But, for some reason, you want to revert the database to the previous state. In this case, use the update-database command to revert the database to the specified previous migration snapshot.
Package Manager Console
PM> Update-database MyFirstMigration
CLI
dotnet ef database update MyFirstMigration.
The above command will revert the database based on a migration named MyFirstMigration and remove all the changes applied for the second migration named MySecondMigration. This will also remove MySecondMigration entry from the __EFMigrationsHistory table in the database.
Note: This will not remove the migration files related to MySecondMigration. Use the remove commands to remove them from the project.
Upvotes: 2
Reputation: 4206
You should delete migration '20160703192724_MyFirstMigration' record from '_EFMigrationsHistory' table.
otherwise below command will remove migration and delete migrations folder:
PMC Command:
> remove-migration -force
CLI Command:
> dotnet ef migrations remove -f
Upvotes: 45
Reputation: 203
dotnet ef database update <the-migration-you-want-to-recover>
dotnet ef migrations remove
Don't forget the remove-Call since this will remove the migration files for you and update the Snapshot-file.
Upvotes: 8
Reputation: 2753
To revert all the migrations which are applied to DB simply run:
update-database 0
It should be followed with running Remove-Migration
as many times as there are migration files visible in the Migration directory. The command deletes the latest migration and also updates the snapshot.
Upvotes: 8
Reputation: 13227
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
Upvotes: 323
Reputation: 169
at first run the following command :
PM>update-database -migration:0
and then run this one :
PM>remove_migration
Finish
Upvotes: 3
Reputation: 24153
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
Upvotes: 559
Reputation: 3689
In general if you are using the Package Manager Console the right way to remove a specific Migration is by referencing the name of the migration
Update-Database -Migration {Name of Migration} -Context {context}
Another way to remove the last migration you have applied according to the docs is by using the command:
dotnet ef migrations remove
This command should be executed from the developer command prompt (how to open command prompt) inside your solution directory.
For example if your application is inside name "Application" and is in the folder c:\Projects. Then your path should be:
C:\Projects\Application
Upvotes: 10
Reputation: 1673
1.find table "dbo._EFMigrationsHistory", then delete the migration record that you want to remove. 2. run "remove-migration" in PM(Package Manager Console). Works for me.
Upvotes: -1
Reputation: 449
In Package Manager Console:
Update-Database Your_Migration_You_Want_To_Revert_To
More options and explanation on how to revert migrations can be seen here
Upvotes: 13
Reputation: 208
To "unapply" the most (recent?) migration after it has already been applied to the database:
Hope this helps and is applicable to any migration in the project... I tested this out only to the most recent migration...
Happy coding!
Upvotes: 8
Reputation: 5480
To completely remove all migrations and start all over again, do the following:
dotnet ef database update 0
dotnet ef migrations remove
Upvotes: 189
Reputation: 4553
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.
Upvotes: 57