本文翻译自:EF Migrations: Rollback last applied migration?

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-MigrationsRetrieving migrations that have been applied to the target database.
201208012131302_Add-SystemCategory
201207311827468_CategoryIdIsLong
201207232247409_AutomaticMigration
201207211340509_AutomaticMigration
201207200025294_InitialCreatePM> Update-Database -TargetMigration:"CategoryIdIsLong"

(At least I can use just the name, skipping the timestamp...) (至少我可以只使用名称,跳过时间戳...)

Is there an easier way? 有更容易的方法吗?


#1楼

参考:https://stackoom.com/question/nwvD/EF迁移-回滚上一次应用的迁移


#2楼

As of EF 5.0, the approach you describe is the preferred way. 从EF 5.0开始,您描述的方法是首选方法。 One solution would be to create a wrapper PS script that automates the steps above. 一种解决方案是创建一个包装器PS脚本,以自动执行上述步骤。 Additionally, feel free to create a feature request for this, or better yet, take a shot at implementing it! 此外,随时可以为此创建功能请求,或者更好地实现它! http://entityframework.codeplex.com/ http://entityframework.codeplex.com/


#3楼

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: 因此,如果您使用GET-MIGRATIONS,并且发现自己拥有A,B,C,D和E,则使用此命令将回滚E和D以使您到达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). 另外,除非有人可以提出相反的意见,否则我注意到您可以使用序数值和简短的-Target开关(因此-Target与-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!). 上面的0甚至会回滚FIRST迁移( 这是一个破坏性的命令-确保在使用它之前先知道自己在做什么! )-如果您使用上面要求名称为的语法,则无法执行此操作目标迁移(应用迁移之前,第0个迁移的名称不存在!)。 So in that case, you have to use the 0 (ordinal) value. 因此,在这种情况下,您必须使用0(常规)值。 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. 同样,如果您已应用迁移A,B,C,D和E(按此顺序),则序数1应该引用A,序数2应该引用B,依此类推。 So to rollback to B you could use either: 因此,要回滚到B,您可以使用以下任一方法:

Update-Database -TargetMigration:"B"

or 要么

Update-Database -TargetMigration:2

Edit October 2019: 编辑2019年10月:

According to this related answer on a similar question, correct command is -Target for EF Core 1.1 while it is -Migration for EF Core 2.0. 根据类似问题的相关答案 ,对于EF Core 1.1,正确的命令是-Target ,对于EF Core 2.0是正确的命令-Migration


#4楼

解决方案是:

Update-Database –TargetMigration 201609261919239_yourLastMigrationSucess

#5楼

In EntityFrameworkCore : EntityFrameworkCore中 :

Update-Database 20161012160749_AddedOrderToCourse

where 20161012160749_AddedOrderToCourse is a name of migration you want to rollback to. 其中20161012160749_AddedOrderToCourse是要回滚到的迁移的名称。


#6楼

Additional reminder: 附加提醒:

If you have multiple configuration type, you need to specify the [ConfigurationName] 如果您有多种配置类型,则需要指定[ConfigurationName]

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

EF迁移:回滚上一次应用的迁移?相关推荐

  1. 如何回滚特定的迁移?

    我有以下迁移文件db\\migrate\\20100905201547_create_blocks.rb 如何专门回滚该迁移文件? #1楼 您可以使用具有不同选项的rake db:rollback来回 ...

  2. 【java基础】子线程任务发生异常,主线程事务如何回滚?

    文章目录 一.提出问题 二.主线程与子线程 三.线程池 四.异常的捕获 五.事务的回滚 一.提出问题 最近有一位朋友问了我这样一个问题,问题的截图如下: 这个问题问的相对比较笼统,我来稍微详细的描述下 ...

  3. git 回滚 merge操作

    1.只在本地 直接reset 到上一个log 就行了. git reset --hard {上个版本commit log} 2.已经推送到了远程 在本地执行1 的操作后,强制推送到远程 git res ...

  4. oracle+12c+大表,ORACLE 12C新特性-在线迁移表或分区 | 信春哥,系统稳,闭眼上线不回滚!...

    今天测试一下ORACLE 12C的一个新功能-在线移动表或分区,这其实在10g版本就已经支持了,难道是官方文档写错了,先不按照文档描述的去测试,看看是否堆表也可以在线迁移.以下是实验过程: 下面先看下 ...

  5. SpringCloud 应用在 Kubernetes 上的最佳实践 — 线上发布(可回滚)

    作者 | 长门 **导读:**本篇是<SpringCloud 应用在 Kubernetes 上的最佳实践>系列文章的第七篇,主要介绍了新功能上线时,如何尽快减少对线上用户的影响?发布系统需 ...

  6. springboot事务回滚源码_002 | 搭上SpringBoot事务源码分析专车

    发车啦,发车啦,上车要求: 点击左上方的"java进阶架构师"进入页面 选择右上角的"置顶公众号"上车 专车介绍 该趟专车是开往Spring Boot事务源码分 ...

  7. matlab 回退到上一行,对乒乓球回滚的分析与MATLAB模拟

    要想让图动起来,就需要一个容易操控的静态图,之后,改变其中的参数,使之伴随时间变化,就可以达到动画的效果,比如这样: [ps,ts]=meshgrid(linspace(0,2*pi,100),lin ...

  8. K8s上的Go服务怎么扩容、发版更新、回滚、平滑重启?教你用Deployment全搞定!

    经过前面不少文章的铺垫,终于可以写这个大家都感兴趣的话题了,在前面两篇文章,我们讲了Kubernetes里的 Pod和 副本集ReplicaSet (RS) 这两个API对象.知道了Pod是Kuber ...

  9. GitHub Desktop 上实现项目的回滚操作,详细介绍

    原创博文,欢迎转载,转载时请务必附上博文链接,感谢您的尊重. 前言 通过本篇,你将学会在 GitHub Desktop (GitHub 桌面版)上,实现[版本的回滚]操作. 控制台代码实现 GitHu ...

最新文章

  1. 110道 Redis面试题及答案(最新整理)
  2. 互联网协议 — 互联网的技术发展
  3. 据说这是程序员为什么改行送外卖的原因
  4. 订阅号助手App发布 手机也能管理公众号了
  5. Spring Boot与数据访问
  6. map mybatis 的字段返回0_Mybatis 你踩过坑吗?
  7. C语言指针和数组的天生姻缘
  8. 搭建DNS域名解析服务器和本地配置HOST文件有什么区别?
  9. 单文件组件的组件传值_移动端组件化架构(下)
  10. c# 批量mqtt_C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整Demo下载)
  11. android百度导航实现,Android 集成百度地图实现设备定位
  12. docker mysql node_Docker打包nodejs项目和数据库
  13. Win10电脑系统文件损坏怎么修复
  14. AE “每用户订阅上的所有人SID 不存在”
  15. CoreOS镜像更新
  16. 【STC单片机】STC15单片机读取MPU6050模块数据并串口输出
  17. android图片文字识别器,图片转换文字识别器
  18. MySQL中json_extract函数说明
  19. 33c3 CTF web WriteUp
  20. AD8226+AD5293

热门文章

  1. SecureCRT连接虚拟机
  2. 手把手教你AndroidStudio多渠道打包
  3. php邮件发送失败,php服务器发送邮件失败问题解析
  4. Android之如何优雅的管理ActionBar
  5. Android之四大组件(Activity)
  6. UIGestureRecognizer手势介绍
  7. (0078)iOS开发之支付宝集成:客户端签名与验证
  8. (0002) iOS 开发之开发者iOS 10 正式版体验报告
  9. codeforces364D
  10. vi使用技巧(转载)