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

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

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"

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

有没有更简单的方法?


当前回答

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

dotnet ef migrations list
dotnet ef database update NameOfYourMigration

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

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

dotnet ef migrations remove

其他回答

在EntityFrameworkCore:

Update-Database 20161012160749_AddedOrderToCourse

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

解决方案是:

Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess

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

dotnet ef migrations list
dotnet ef database update NameOfYourMigration

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

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

dotnet ef migrations remove
Update-Database –TargetMigration:"Your migration name"

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

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

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

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

or

Update-Database –TargetMigration:Your_Migration_Index -force