[英]How to unapply a migration in ASP.NET Core with EF Core
When I run PM> Remove-Migration -context BloggingContext
in VS2015 with an ASP.NET Core project using EF Core I get the following error:当我使用 EF Core 使用 ASP.NET 核心项目在 VS2015 中运行
PM> Remove-Migration -context BloggingContext
时,我收到以下错误:
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.
我正在使用最新版本的 ASP.NET Core 1.0、EF Core 和 VS2015 Update 3。
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如果使用 PMC,请尝试: PM> update-database 0 这将擦除数据库并允许您删除解决方案上的迁移快照
To completely remove all migrations and start all over again, do the following:要完全删除所有迁移并重新开始,请执行以下操作:
dotnet ef database update 0
dotnet ef migrations remove
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.只需删除
Migrations
文件夹。
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.没有删除一堆迁移的命令,我们不能只删除这几个
migrations
及其*.designer.cs
文件,因为我们需要保持快照文件处于一致状态。 We need to remove migrations one by one (see To remove last migration
above).我们需要一个一个地删除迁移(请参阅上面的
To remove last migration
)。
To unapply and remove last migration:要取消应用和删除上次迁移:
dotnet ef migrations remove --force
or
PM> Remove-Migration -Force
To revert the last applied migration you should (package manager console commands):要恢复上次应用的迁移,您应该(包管理器控制台命令):
PM> Update-Database <prior-migration-name>
PM> Update-Database <prior-migration-name>
PM> Remove-Migration
PM> Remove-Migration
UPD : The second step seems to be not required in latest versions of Visual Studio (2017). UPD :最新版本的 Visual Studio (2017) 似乎不需要第二步。
You can still use the Update-Database
command.您仍然可以使用
Update-Database
命令。
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.您应该能够从数据库中的
__MigrationHistory
表中删除该条目,然后再次运行Remove-Migration
命令。 You could also delete the migration file and just start again.您也可以删除迁移文件并重新开始。
You should delete migration '20160703192724_MyFirstMigration' record from '_EFMigrationsHistory' table.您应该从“_EFMigrationsHistory”表中删除迁移“20160703192724_MyFirstMigration”记录。
otherwise below command will remove migration and delete migrations folder:否则下面的命令将删除迁移并删除迁移文件夹:
PMC Command: PMC 命令:
> remove-migration -force
CLI Command: CLI 命令:
> dotnet ef migrations remove -f
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.注意这是在您想要清除数据库时使用的。
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 可以在此处查看有关如何还原迁移的更多选项和说明
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!快乐编码!
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.例如,如果您的应用程序在名称“Application”内并且位于文件夹 c:\Projects 中。 Then your path should be:
那么你的路径应该是:
C:\Projects\Application
To revert all the migrations which are applied to DB simply run:要恢复应用于 DB 的所有迁移,只需运行:
update-database 0
It should be followed with running Remove-Migration
as many times as there are migration files visible in the Migration directory.在 Migration 目录中可以看到迁移文件时,应在其后多次运行
Remove-Migration
。 The command deletes the latest migration and also updates the snapshot.该命令会删除最新的迁移并更新快照。
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.不要忘记 remove-Call,因为这将为您删除迁移文件并更新快照文件。
I don't understand why we are confusing things up here.我不明白为什么我们在这里混淆了事情。 So I'll write down a clear explanation, and what you have to notice .
所以我会写一个清晰的解释,以及你需要注意的地方。
All the commands will be written using dotnet
.所有命令都将使用
dotnet
编写。
This solution is provided for .net Core 3.1, but should be compatible with all other generations as well此解决方案是为 .net Core 3.1 提供的,但也应与所有其他代兼容
cd to_your_project
then dotnet ef migrations remove
cd to_your_project
然后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 database update
或在 c# 代码中调用Database.Migrate()
时,删除迁移才有效,换句话说,仅当迁移尚未应用于您的数据库时。
dotnet ef migrations add <your_changes>
and apply it, which is recommended by microsoft.dotnet ef migrations add <your_changes>
并应用它,这是微软推荐的。dotnet ef database update <your_migration_name_to_jump_back_to>
dotnet ef database update <your_migration_name_to_jump_back_to>
将数据库更新到指定的迁移(基本上是取消应用或还原未选择的迁移) Note: if the migration you want to unapply, does not contain a specific column or table, which are already in your database applied and being used, the column or table will be dropped, and your data will be lost.注意:如果您要取消应用的迁移不包含特定的列或表,而这些列或表已在您的数据库中应用并正在使用,则该列或表将被删除,您的数据将丢失。
After reverting the migration, you can remove your unwanted migration还原迁移后,您可以删除不需要的迁移
at first run the following command :首先运行以下命令:
PM>update-database -migration:0
and then run this one :然后运行这个:
PM>remove_migration
Finish结束
In order to unapply a migration in EF Core 1.0 use the command:要在 EF Core 1.0 中取消应用迁移,请使用以下命令:
dotnet ef database update {migration_name} dotnet ef 数据库更新 {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 dotnet ef 迁移列表
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 PM> 移除迁移
CLI> dotnet ef migrations remove CLI> dotnet ef 迁移删除
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.假设您更改了域类并使用 add-migration 命令创建了名为 MySecondMigration 的第二个迁移,并使用 Update 命令将此迁移应用于数据库。 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.
在这种情况下,请使用 update-database 命令将数据库恢复到指定的先前迁移快照。
Package Manager Console包管理器控制台
PM> Update-database MyFirstMigration PM> 更新数据库 MyFirstMigration
CLI命令行界面
dotnet ef database update MyFirstMigration.
dotnet ef 数据库更新 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.上述命令将基于名为 MyFirstMigration 的迁移还原数据库,并删除应用于名为 MySecondMigration 的第二个迁移的所有更改。 This will also remove MySecondMigration entry from the __EFMigrationsHistory table in the database.
这还将从数据库中的 __EFMigrationsHistory 表中删除 MySecondMigration 条目。
Note: This will not remove the migration files related to MySecondMigration.注意:这不会删除与 MySecondMigration 相关的迁移文件。 Use the remove commands to remove them from the project.
使用删除命令将它们从项目中删除。
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
.因为我已被重定向到此问题以搜索有关
ef
而不是ef core
中的回滚迁移,所以我将这个答案添加到每个想了解ef
中相同问题的人的问题中。 If you're using ef
you can use the following command to roll back the migration:如果您使用的是
ef
,则可以使用以下命令回滚迁移:
Update-Database [[-Migration] <String>]
[-Context <String>]
[-Project <String>]
[-StartupProject <String>]
[<CommonParameters>]
Based on question:基于问题:
Update-Database -Migration <previous-migration-name> -context BloggingContext
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
例如:
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.注意:由于我们有两次错误的迁移,请运行两次
Remove Migration
命令。
If you haven't applied the migration to the db如果您尚未将迁移应用到数据库
Just run:赶紧跑:
Remove-Migration
In our case run the Remove Migration
command twice.在我们的例子中,运行两次
Remove Migration
命令。
Hope this helps someone.希望这可以帮助某人。
var context = serviceProvider.GetRequiredService<ApplicationDbContext>();
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roleManaget = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
await context.Database.EnsureDeletedAsync();
Really?真的吗? all the top answers here don't work, Here is what did work: In the Package manager console:
这里所有最重要的答案都不起作用,这是起作用的:在 Package 管理器控制台中:
Update-Database 0更新数据库 0
this reverts all migrations on the database end.(Maybe its not what you want, but if you are like me and all you had anyways was the initial migration, this would work fine)这将恢复数据库端的所有迁移。(也许它不是你想要的,但如果你像我一样,无论如何你所拥有的只是初始迁移,这会很好)
Next command is:下一个命令是:
remove-migration移除迁移
The command defaults to your last migration so for initial migration it would remove it.该命令默认为您的最后一次迁移,因此对于初始迁移,它将删除它。 by the way you can do Update-Database --help to get more info on commands.
顺便说一句,您可以执行Update-Database --help以获取有关命令的更多信息。
I'm using.Net6 so maybe that is why things were different than what other people wrote here.我正在使用.Net6,所以也许这就是为什么事情与其他人在这里写的不同。
1.find table "dbo._EFMigrationsHistory", then delete the migration record that you want to remove. 1.找到表“dbo._EFMigrationsHistory”,然后删除要删除的迁移记录。 2. run "remove-migration" in PM(Package Manager Console).
2. 在 PM(Package Manager Console) 中运行“remove-migration”。 Works for me.
为我工作。
You can do it with:你可以这样做:
dotnet ef migrations remove
Take care not to remove any migrations which are already applied to production databases.注意不要删除任何已应用于生产数据库的迁移。 Not doing so will prevent you from being able to revert it, and may break the assumptions made by subsequent migrations.
不这样做会阻止您恢复它,并且可能会破坏后续迁移所做的假设。
in Package Manager Console, just Type Remove-Migration And Press Enter.在包管理器控制台中,只需键入Remove-Migration并按 Enter。 It automatically removes the migration.
它会自动删除迁移。
Tools -> Nuget Package Manager -> Package Manager Console工具 -> Nuget 包管理器 -> 包管理器控制台
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.