EF架构~CodeFirst⽣产环境的Migrations
Migrations即迁移,它是EF的code first模式出现的产物,它意思是说,将代码的变化反映到数据库上,这种反映有两种环境,⼀是本地开发环境,别⼀种是服务器的⽣产环境,本地开发环境主要使⽤包管理⼯具的update-database即可完成数据库的迁移(变更),⽽在⽣产环境就显得⿇烦⼀些,因为你不会在⽣产环境放程序源代码和VS开发⼯具,哈哈.
服务器上⽣产环境的数据迁移
如果我们有迁移⽂件如下
{
/// <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");
}
settings中文意思}
在本地VS中执⾏下⾯命令,它将⽣产迁移计划,即SQL语句
Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration:manage
它将⽣成对应的SQL脚本,我们在服务器上运⾏即可
幂等性
由Migration⽣成的SQL脚本是幂等的,即,当你多次执⾏SQL脚本时,产⽣的结果是⼀样的,不对有负作⽤.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论