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完成以后执行的插入数据库的操作,可以放一些系统运行所需要的必须得数据

 internal sealed class Configuration : DbMigrationsConfiguration<Infrastructor.MainBoundedContext.UnitWorks.MainDBUnitWorkContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "Infrastructor.MainBoundedContext.UnitWorks.MainDBUnitWorkContext";
} protected override void Seed(Infrastructor.MainBoundedContext.UnitWorks.MainDBUnitWorkContext context)
{
// This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
context.Status.AddOrUpdate(p => p.Name,
new Status { Name = "通过" },
new Status { Name = "等待审批" }
);
}
}

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

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

> Add-Migration "MyTest"

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

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

Entity Framewor中的 Migration的更多相关文章

  1. Entity Framework中的Migration问题

    1.自从用上了Entity Framework(简称EF),妈妈再也不用担心我要写那么复杂的SQL语句了! 这是微软新一代的ORM工具,它能够将数据库的表中的记录映射成为程序中的一个对象,当然也能够将 ...

  2. [EF] 如何在 Entity Framework 中以手动方式设定 Code First 的 Migration 作业

    Entity Framework (简称 EF) 发展到现在, 版本已经进入 6.1.0, 距离我写的「在 VS2013 以 Code First 方式建立 EF 资料库」这篇文章已有半年的时间.如果 ...

  3. Entity Framework 教程——Entity Framework中的实体类型

    Entity Framework中的实体类型 : 在之前的章节中我们介绍过从已有的数据库中创建EDM,它包含数据库中每个表所对应的实体.在EF 5.0/6.0中,存在POCO 实体和动态代理实体两种. ...

  4. 关于Entity Framework中的Attached报错相关解决方案的总结

    关于Entity Framework中的Attached报错的问题,我这里分为以下几种类型,每种类型我都给出相应的解决方案,希望能给大家带来一些的帮助,当然作为读者的您如果觉得有不同的意见或更好的方法 ...

  5. 关于Entity Framework中的Attached报错的完美解决方案终极版

    之前发表过一篇文章题为<关于Entity Framework中的Attached报错的完美解决方案>,那篇文章确实能解决单个实体在进行更新.删除时Attached的报错,注意我这里说的单个 ...

  6. [转]在Entity Framework中使用LINQ语句分页

    本文转自:http://diaosbook.com/Post/2012/9/21/linq-paging-in-entity-framework 我们知道,内存分页效率很低.并且,如果是WebForm ...

  7. 在Entity Framework中使用事务

    继续为想使用Entity Framework的朋友在前面探路,分享的东西虽然技术含量不高,但都是经过实践检验的. 在Entity Framework中使用事务很简单,将操作放在TransactionS ...

  8. Entity Framework中的多个库操作批量提交、事务处理

    在Entity Framework 中使用SaveChanges()是很频繁的,单次修改或删除数据后调用SaveChanges()返回影响记录数. 要使用批量修改或者批量删除数据,就需要SaveCha ...

  9. LinqToSql和ASP.NET Entity FrameWork 中使用事务

    ASP.NET Entity FrameWork中: int flag = -1; if (this.URPmanagementEntities1.Connection.State != System ...

随机推荐

  1. centos搭建zabbix

    安装一些依赖包,不同情况缺的包不同 yum install mysql-devel curl curl-devel net-snmp net-snmp-devel perl-DBI php-gd ph ...

  2. sql每五秒插入一条数据 一次插入N条数据

    1建立数据表 create table projectManage ( ID int identity primary key not null, projectName )not null, man ...

  3. JS笔试题

    JS 引用相关题目 以下代码输出什么? 为什么? var a = {n:1}; var b = a; a = {n:2}; a.x = a ; console.log(a.x); console.lo ...

  4. MongoDB insert performance rapidly dropping

    http://dba.stackexchange.com/questions/65554/mongodb-insert-performance-rapidly-dropping http://www. ...

  5. 微软的OneDrive研究~

    Dropbox 很好,唯一觉得不爽的是只能同步指定的目录.不过被墙之后就不那么方便了,所以改用微软的 Live Mesh,缺点是支持的设备少(仅 PC 和 Mac). https://technet. ...

  6. 把所有的QT的类都过一遍脑子

    http://doc.qt.io/qt-5/classes.html 这样就明白QT提供的全部功能了,避免重复造轮子,或者给自己开发带来麻烦-

  7. javascript之Date

    JSON 日期转 JS日期,我们知道,日期类型转成JSON之后,返回的数据类似这样: /Date(1379944571737)/ 但是这种日期并不能直接显示,因为根本没有人知道这是什么意思,下面提供一 ...

  8. Intersection of Two Linked Lists 解答

    Question Write a program to find the node at which the intersection of two singly linked lists begin ...

  9. 关闭ubuntu apport

    apport就是ubuntu上的"crash report"服务,就是当有程序崩溃时弹出的那个发送error report的程序: 个人觉得此功能无用,本着给我的老本子节省资源的思 ...

  10. Java根据年份算出所属的生肖。

    一个小程序~ public String getYear(Integer year){ if(year<1900){ return "未知"; } Integer start ...