这看起来是一个非常普通的任务,但我找不到一个简单的方法来做。

我想撤消上一次应用的迁移。我本以为会有个简单的命令,比如

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

相反,我能想到的只有:

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"

(至少我可以只使用名称,跳过时间戳…)

有没有更简单的方法?


当前回答

如果存在数据丢失的可能性,EF不会完成update-database命令,因为默认情况下automaticmigrationdatalossallows = false,并回滚该操作,除非您使用-force参数运行它。

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

or

Update-Database –TargetMigration:Your_Migration_Index -force

其他回答

在EntityFrameworkCore:

Update-Database 20161012160749_AddedOrderToCourse

其中20161012160749_AddedOrderToCourse是您想要回滚到的迁移名称。

额外提示:

如果您有多种配置类型,您需要指定[ConfigurationName]

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

我意识到使用CLI的dotnet命令没有任何好的解决方案,所以这里有一个:

dotnet ef migrations list
dotnet ef database update NameOfYourMigration

在NameOfYourMigration的位置输入您想要恢复到的迁移的名称。

然后您可以删除所有恢复的迁移,以便更好地使用

dotnet ef migrations remove

英孚的核心

更新数据库到上一个点

update-database CategoryIdIsLong

然后,去除不好的迁移

remove-migration

我使用EntityFrameworkCore,我使用@MaciejLisCK的答案。如果你有多个DB上下文,你还需要通过添加context参数来指定上下文,例如:

Update-Database 201207211340509_MyMigration

(其中201207211340509_MyMigration是你想要回滚到的迁移,myDBcontext是你的DB上下文的名称)