EF Code First 数据库迁移Migration剖析
1.简介
Entity Framework 的Code First 方式,提供了一种方式:编写模型Model,生成模型变更,根据模型变更修改数据库。
而其所以来的环境就是强大的Nuget,如果还在是VS2010一下的同学,请不要往下看了,将无一益处。
2.操作步骤
1)建立或修改Model,即实体类;
这里演示修改:
public class BootStrapLists
{
public int ID { get; set; }
public string Title { get; set; } [DataType(DataType.MultilineText)]
public string Description { get; set; } /// <summary>
/// 新增一个时间字段
/// </summary>
public DateTime CreateDate { get; set; }
}
2)建立或修改ModelMap;
public class BootStrapListsMap : EntityTypeConfiguration<BootStrapLists>
{
public BootStrapListsMap()
{
ToTable("");
HasKey(zw => zw.ID).Property(zw => zw.ID).HasColumnName("ID");
Property(zw => zw.Title).HasColumnName("Title");
Property(zw => zw.Description).HasColumnName("Description");
//新增
Property(zw => zw.CreateDate).HasColumnName("CreateDate");
}
}
3)在OnModelCreate时增加Map;
public DbSet<BootStrapLists> BootStrapLists { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new BootStrapListsMap());
//dynamically load all configuration
4)使用命令Add-Migration
如果是第一次使用,则请先使用:
Enable-Migrations
在生成了
红色框所示的文件和文件夹
继续使用命令:
Add-Migration Tests
其中,Tests为自定义名称;

生成迁移文件:

5)使用命令Update-DataBase
Update-DataBase
迁移原理:查询数据库的表,__MigrationHistory,遍历Migrations文件夹下的所有文件,如果文件不在__MigrationHistory表内,那么就执行迁移。
Eg: 如果20141104233562_Tests不在数据库内,则执行迁移。
11)初始数据库:

22)执行:

33)执行之后的数据库:

修改表的结果:

6)深究
public partial class Tests : DbMigration
{
public override void Up()
{
AddColumn("dbo.BootStrapLists", "CreateDate", c => c.DateTime(nullable: true));
} public override void Down()
{
}
}
类,DbMigration中邮很多API,可以直接修改数据库!
EF Code First 数据库迁移Migration剖析的更多相关文章
- 3.3 使用Code First数据库迁移
当Entity Framework Code First的数据模型发生异动时,默认会引发一个System.InvalidOpertaionException异常.一种解决方法是在Global.asax ...
- EF Code First 数据迁移操作
打开执行命令窗体 1.EF Code First创建数据库 PM> Install-Package EntityFramework 2.EF Code First数据库迁移 2.1.生成数据库 ...
- Code First 数据库迁移
当 Entity Framework Code First 的数据模型发生改变时,默认会引发一个System.InvalidOperationException 的异常.解决方法是使用DropCrea ...
- 使用 Code First 数据库迁移
当 Entity Framework Code First 的数据模型发生改变时,默认会引发一个System.InvalidOperationException 的异常.解决方法是使用DropCrea ...
- Entity Framework 5.0系列之Code First数据库迁移
我们知道无论是"Database First"还是"Model First"当模型发生改变了都可以通过Visual Studio设计视图进行更新,那么对于Cod ...
- EF Code First数据库映射规则及配置
EF Code First数据库映射规则主要包括以下方面: 1.表名及所有者映射 Data Annotation: 指定表名 1 using System.ComponentModel.DataAnn ...
- EF Code First 数据迁移配置
这里我想讲清楚code first 数据迁移的两种模式,还有开发环境和生产环境数据迁移的最佳实践. 1.1 数据迁移综述 EF Code first 虽然已经有了几种不同的数据库初始化策略,但是大部分 ...
- MVC VS2012 Code First 数据库迁移教程
1.在“服务资源管理器”连接数据库 2.打开工具-Nuget程序包管理器“程序包管理器控制台” 3.控制台输入命令:PM> Enable-Migrations -StartUpProjectNa ...
- ASP.NET MVC 4下 Code First 数据库迁移
一.命令开启 1.打开控制台:视图->其他窗口->程序包管理器控制台: 2.启动数据库迁移,执行命令:enable-migrations 创建成功后会新增migrations目录等. 若 ...
随机推荐
- 免费馅饼 Why WA
免费馅饼 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1576 Solved: 577 Description 都说天上不会掉馅饼,但有一天gameb ...
- Fifth scrum meeting - 2015/10/30
概述 从昨天开始,我们的开发工作终于进入了正轨,由于之前没有mooc服务器API接口,一些工作无法进行. 因为我们团队开始开发较晚,因此我们将开发阶段的截至时间定为了下周五,测试阶段则压缩为下周周六和 ...
- 基于2d Tool Kit 精灵合图,动作生成工具
http://blog.csdn.net/onerain88/article/details/18563687 2d Tool Kit 是一款出色的基于unity3d 开发2d游戏的工具,提供了丰富的 ...
- index and polymorphic
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations class CreateStars < ...
- 安装cuda时 提示toolkit installation failed using unsupported compiler解决方法
在安装cuda的时候,有时候会提示toolkit installation failed using unsupported compiler.这是因为GCC版本不合适所导致的. 解决的方法很简单,直 ...
- 解决php configure: error: Cannot find ldap libraries in /usr/lib.错误
解决php configure: error: Cannot find ldap libraries in /usr/lib.错误 iitshare 分类:Linux,PHP,项目实施 | 标签:Ca ...
- Fast Power
Calculate the a^n % b where a, b and n are all 32bit integers. Example For 2^31 % 3 = 2 For 100^1000 ...
- 【leetcode】Subsets
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- python文件取MD5
import hashlib def md5sum(filename, blocksize=65536): hash = hashlib.md5() with open(filename, " ...
- ubuntu maven环境安装配置
转载地址:http://my.oschina.net/hongdengyan/blog/150472#OSC_h1_4 一.环境说明: 操作系统:Ubuntu 12.04.2 LTS maven:ap ...