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

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

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

其他回答

英孚的核心

PM>更新数据库yourMigrationName

(还原迁移)

点> - database就

为我工作

在这种情况下,原来的问题 (yourMigrationName = CategoryIdIsLong)

英孚的核心

更新数据库到上一个点

update-database CategoryIdIsLong

然后,去除不好的迁移

remove-migration

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

Update-Database 201207211340509_MyMigration

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

在EntityFrameworkCore:

Update-Database 20161012160749_AddedOrderToCourse

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

解决方案是:

Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess