EF架构~CodeFirst生产环境的Migrations

回到目录

Migrations即迁移,它是EF的code first模式出现的产物,它意思是说,将代码的变化反映到数据库上,这种反映有两种环境,一是本地开发环境,别一种是服务器的生产环境,本地开发环境主要使用包管理工具的update-database即可完成数据库的迁移(变更),而在生产环境就显得麻烦一些,因为你不会在生产环境放程序源代码和VS开发工具,哈哈.

本地数据库迁移请看我的这篇文章

EF架构~CodeFirst数据迁移与防数据库删除

服务器上生产环境的数据迁移

如果我们有迁移文件如下

复制代码
public partial class manager : DbMigration
    {
  /// <summary>
        /// 要在升级过程中执行的操作。
        /// </summary>
public override void Up() { DropForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls"); DropForeignKey("dbo.WebDataSettings", "DepartmentId", "dbo.WebDepartments"); RenameColumn(table: "dbo.WebDataSettings", name: "DepartmentId", newName: "WebDepartmentsId"); RenameIndex(table: "dbo.WebDataSettings", name: "IX_DepartmentId", newName: "IX_WebDepartmentsId"); AddForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls", "ID", cascadeDelete: true); AddForeignKey("dbo.WebDataSettings", "WebDepartmentsId", "dbo.WebDepartments", "ID", cascadeDelete: true); }
/// <summary>
        /// 要在降级过程中执行的操作。
        /// </summary>
public override void Down() { DropForeignKey("dbo.WebDataSettings", "WebDepartmentsId", "dbo.WebDepartments"); DropForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls"); RenameIndex(table: "dbo.WebDataSettings", name: "IX_WebDepartmentsId", newName: "IX_DepartmentId"); RenameColumn(table: "dbo.WebDataSettings", name: "WebDepartmentsId", newName: "DepartmentId"); AddForeignKey("dbo.WebDataSettings", "DepartmentId", "dbo.WebDepartments", "ID"); AddForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls", "ID"); } }
复制代码

在本地VS中执行下面命令,它将生产迁移计划,即SQL语句

Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration:manage

它将生成对应的SQL脚本,我们在服务器上运行即可

幂等性

Migration生成的SQL脚本是幂等的,即,当你多次执行SQL脚本时,产生的结果是一样的,不对有负作用.

回到目录

posted @   张占岭  阅读(5254)  评论(2编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2013-07-27 品味编程~底层开发人员应该这样设计一个字体类
2012-07-27 代码重构~提取到类
2011-07-27 不忘本~浅拷贝和深拷贝
2011-07-27 用Jquery自己开发个代阴影的对话框吧!
2011-07-27 不忘本~结构
2011-07-27 数据结构~在页面上渲染树型结构
2011-07-27 数据结构~时间复杂度和空间复杂度
点击右上角即可分享
微信分享提示