博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framewor中的 Migration
阅读量:4314 次
发布时间:2019-06-06

本文共 2165 字,大约阅读时间需要 7 分钟。

http://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx = Code based Migration

http://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx = Automated Migration

 

上边两篇文章分别介绍了DataMigration的两种方法。 下面简单总结一下步骤供自己使用。

VS Tools->package manager console

> enble-migration 

执行上面步骤将在你的DBContext目录下生成一个Migrations/Configuration.cs文件。这个文件的。 Seed方法是当Migration完成以后执行的插入数据库的操作,可以放一些系统运行所需要的必须得数据

1 internal sealed class Configuration : DbMigrationsConfiguration
2 { 3 public Configuration() 4 { 5 AutomaticMigrationsEnabled = true; 6 ContextKey = "Infrastructor.MainBoundedContext.UnitWorks.MainDBUnitWorkContext"; 7 } 8 9 protected override void Seed(Infrastructor.MainBoundedContext.UnitWorks.MainDBUnitWorkContext context)10 {11 // This method will be called after migrating to the latest version.12 13 // You can use the DbSet
.AddOrUpdate() helper extension method 14 // to avoid creating duplicate seed data. E.g.15 //16 // context.People.AddOrUpdate(17 // p => p.FullName,18 // new Person { FullName = "Andrew Peters" },19 // new Person { FullName = "Brice Lambson" },20 // new Person { FullName = "Rowan Miller" }21 // );22 //23 context.Status.AddOrUpdate(p => p.Name,24 new Status { Name = "通过" },25 new Status { Name = "等待审批" }26 );27 }28 }

将以上构造函数Merge到自己的DBCOntext文件中

public MainDBUnitWorkContext(string connectionString): base(connectionString){//this.Configuration.ProxyCreationEnabled = false;this.Configuration.LazyLoadingEnabled = true;Database.SetInitializer(new MigrateDatabaseToLatestVersion
("MyDBConnectionString"));}

 

> Add-Migration "MyTest"

添加新的migration,执行完成以后在migration目录下生成一个 20150101_Mytest.cs文件,其内部列举了这次DB Upgrade执行的升级和降级的操作

> Update-DataBase -script: 参数script 是执生成sql脚本,也可以不要这个参数,将直接更新数据库

 

转载于:https://www.cnblogs.com/ygshen/p/4533184.html

你可能感兴趣的文章
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>