EF Core 小技巧:迁移已经应用到数据库,如何进行迁移回退操作?
2024-10-19 14:34:56
场景描述:项目中存在两个迁移 Teacher
和 TeachingPlan
,TeachingPlan
在 Teacher
之后创建,并且已经执行 dotnet ef database update
将新迁移应用到数据库。此时,因为实体修改,我们希望删除 TeachingPlan
迁移然后创建新的 TeachingPlan
迁移,该怎么办?
示例:查看迁移列表
dotnet ef migrations list
Build started...
Build succeeded.
20211026071835_Teacher
20211108141706_TeachingPlan
分析:直接移除 TeachingPlan
行不行?我们先试试。
示例:移除最后一个迁移
dotnet ef migrations remove
Build started...
Build succeeded.
The migration '20211108141706_TeachingPlan' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration instead.
移除失败:迁移已经应用到数据库。如果该迁移已经应用于其他数据库,请考虑用一个新的迁移来恢复其变化。
根据错误提示,建议我们再创建一个新的迁移,比如:TeachingPlan_01
,这样能够达到效果,但是不够简洁,存在多个作用相同的迁移。
重新梳理思路:在移除迁移之前,先将数据库恢复到数据迁移之前的状态。使用 dotnet ef database update [miagration_anme]
可以将数据库架构恢复到指定迁移时的状态。
示例:将数据库恢复到 Teacher
dotnet ef database update Teacher
然后移除 TeachingPlan 迁移
dotnet ef migrations remove
移除成功!此时迁移和数据库结构都恢复到 Teacher 状态,再重新创建迁移:
dotnet ef migrations add TeachingPlan
迁移回退任务完成!
小结:
- 迁移具有前后连贯性,迁移和数据架构应保持一致性。应避免删除已应用到生产数据库的任何迁移。
dotnet ef migrations remove
不带参数,每执行一次,则移除最新创建的迁移。dotnet ef database update
可以将数据库更新到任何一个指定迁移时的架构。
最新文章
- php之CI框架多语言的用法
- centos BIND服务基础及域主服务器配置
- 03 javadoc
- dvd的舞女
- spring问题org.springframework.beans.factory.CannotLoadBeanClassException
- C#运算符的优先级
- UNIX网络编程---简介
- windows下fitness python版本安装测试
- 比较优势 - MBA智库百科
- 关于Cassandra与Thrift在int/text/varint上的暧昧
- Spring切面通知执行的顺序(Advice Order)
- Vulkan Tutorial 26 view and sampler
- Maven(八)Eclipse创建Web项目(复杂方式)
- SQL Server中如何定位Row Lock锁定哪一行数据
- 分布式架构探索 - 1. RPC框架之Java原生RMI
- CentOS 6.8 部署django项目一
- python实现图的遍历(递归和非递归)
- Apache+wsgi+flask部署
- 启动 angular-phonecat 项目时出现这玩意 。('The header content contains invalid characters');
- Oracle备份提示,EXP-00091: 正在导出有问题的统计信息。
热门文章
- Jmeter 压力测试学习8--断言
- Ubuntu安装Oracleclient远程连接数据库
- 势流理论笔记:03 Hess-Smith积分方法
- mysql面试题及答案,mysql最新面试题,mysql面试题大全汇总
- @Value竟然能玩出这么多花样
- 左手IRR,右手NPV,掌握发家致富道路密码
- 如何在 Serverless K8s 集群中低成本运行 Spark 数据计算?
- cf1082D Maximum Diameter Graph(构造+模拟+细节)
- 用C++实现的数独解题程序 SudokuSolver 2.3 及实例分析
- 一个简单的单例模式Demo