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

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

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"

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

有没有更简单的方法?


当前回答

Update-Database –TargetMigration:"Your migration name"

对于这个问题,我建议这个链接:

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

其他回答

英孚的核心

更新数据库到上一个点

update-database CategoryIdIsLong

然后,去除不好的迁移

remove-migration

在EntityFrameworkCore:

Update-Database 20161012160749_AddedOrderToCourse

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

解决方案是:

Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess

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

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

or

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

对于这个问题,我建议这个链接:

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