EF Migrations: Rollback last applied migration?

2019-01-16 00:02发布

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?

7条回答
叼着烟拽天下
2楼-- · 2019-01-16 00:12

In EF Core you can enter the command Remove-Migration in the package manager console after you've added your erroneous migration.

The console suggests you do so if your migration could involve a loss of data:

An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy. To undo this action, use Remove-Migration.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-16 00:13

As of EF 5.0, the approach you describe is the preferred way. One solution would be to create a wrapper PS script that automates the steps above. Additionally, feel free to create a feature request for this, or better yet, take a shot at implementing it! http://entityframework.codeplex.com/

查看更多
Fickle 薄情
4楼-- · 2019-01-16 00:15

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:

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). 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!). So in that case, you have to use the 0 (ordinal) value. 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. So to rollback to B you could use either:

Update-Database -TargetMigration:"B"

or

Update-Database -TargetMigration:2
查看更多
Deceive 欺骗
5楼-- · 2019-01-16 00:22

Additional reminder:

If you have multiple configuration type, you need to specify the [ConfigurationName]

Update-Database -Configurationtypename [ConfigurationName] -TargetMigration [MigrationName]
查看更多
▲ chillily
6楼-- · 2019-01-16 00:23

The solution is:

Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess
查看更多
疯言疯语
7楼-- · 2019-01-16 00:28

In EntityFrameworkCore:

Update-Database 20161012160749_AddedOrderToCourse

where 20161012160749_AddedOrderToCourse is a name of migration you want to rollback to.

查看更多
登录 后发表回答