本文翻译自: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? 有更容易的方法吗?
#1楼
参考:https://stackoom.com/question/nwvD/EF迁移-回滚上一次应用的迁移
#2楼
As of EF 5.0, the approach you describe is the preferred way. 从EF 5.0开始,您描述的方法是首选方法。 One solution would be to create a wrapper PS script that automates the steps above. 一种解决方案是创建一个包装器PS脚本,以自动执行上述步骤。 Additionally, feel free to create a feature request for this, or better yet, take a shot at implementing it! 此外,随时可以为此创建功能请求,或者更好地实现它! http://entityframework.codeplex.com/ http://entityframework.codeplex.com/
#3楼
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: 因此,如果您使用GET-MIGRATIONS,并且发现自己拥有A,B,C,D和E,则使用此命令将回滚E和D以使您到达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). 另外,除非有人可以提出相反的意见,否则我注意到您可以使用序数值和简短的-Target开关(因此-Target与-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!). 上面的0甚至会回滚FIRST迁移( 这是一个破坏性的命令-确保在使用它之前先知道自己在做什么! )-如果您使用上面要求名称为的语法,则无法执行此操作目标迁移(应用迁移之前,第0个迁移的名称不存在!)。 So in that case, you have to use the 0 (ordinal) value. 因此,在这种情况下,您必须使用0(常规)值。 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. 同样,如果您已应用迁移A,B,C,D和E(按此顺序),则序数1应该引用A,序数2应该引用B,依此类推。 So to rollback to B you could use either: 因此,要回滚到B,您可以使用以下任一方法:
Update-Database -TargetMigration:"B"
or 要么
Update-Database -TargetMigration:2
Edit October 2019: 编辑2019年10月:
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 1.1,正确的命令是-Target
,对于EF Core 2.0是正确的命令-Migration
。
#4楼
解决方案是:
Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess
#5楼
In EntityFrameworkCore : 在EntityFrameworkCore中 :
Update-Database 20161012160749_AddedOrderToCourse
where 20161012160749_AddedOrderToCourse
is a name of migration you want to rollback to. 其中20161012160749_AddedOrderToCourse
是要回滚到的迁移的名称。
#6楼
Additional reminder: 附加提醒:
If you have multiple configuration type, you need to specify the [ConfigurationName] 如果您有多种配置类型,则需要指定[ConfigurationName]
Update-Database -Configurationtypename [ConfigurationName] -TargetMigration [MigrationName]