EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/
系列目录:
- Relationship in Entity Framework Using Code First Approach With Fluent API【【使用EF Code-First方式和Fluent API来探讨EF中的关系】】
- Code First Migrations with Entity Framework【使用EF 做数据库迁移】
- CRUD Operations Using Entity Framework 5.0 Code First Approach in MVC【在MVC中使用EF 5.0做增删查改】
- CRUD Operations Using the Repository Pattern in MVC【在MVC中使用仓储模式,来做增删查改】
- CRUD Operations Using the Generic Repository Pattern and Unit of Work in MVC【在MVC中使用泛型仓储模式和工作单元来做增删查改】
- CRUD Operations Using the Generic Repository Pattern and Dependency Injection in MVC【在MVC中使用泛型仓储模式和依赖注入,来做增删查改】
前面的文章中,学习了EF 中的几种关系,一对一,一对多,多对多。但是相信大家肯定会有疑问:
1.我难道每次都要创建数据库么?
2.我怎么样从已经存在的表中,添加字段和移除字段呢?
3.当我向表中,添加字段或者移除字段,我怎么来避免丢失数据呢?
4.当数据库发生改变的时候,我怎么获取到创建数据库的脚本呢?
不用着急,这篇文章,我会向大家一一讲到:
首先,说说我们为什么要使用数据库迁移技术吧,因为我们的实体总是变动地很频繁,在上篇文章中,我们使用了数据库初始化策略来做,也就是每次当数据库不存在的时候,就创建数据库【类似还有几种初始化策略】,然而,当你的实体改变的时候,在使用这个策略,EF就会报错。而数据库迁移技术就可以帮到我们,我们不用每次都创建数据库。并且数据库迁移技术还可以为我们设置初始化的数据。
先看看项目结构吧:
我们需要建2个类库项目,还有一个控制台的程序:
Student类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EF.Core
{
public class Student
{
public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; }
}
}

StudentMap类:

using EF.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Ef.Data
{
public class StudentMap:EntityTypeConfiguration<Student>
{
public StudentMap()
{
this.HasKey(s => s.ID);
this.Property(s => s.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(s => s.Name).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();
this.Property(s => s.Sex).HasColumnType("nvarchar").IsRequired();
this.Property(s => s.Age).IsRequired(); this.ToTable("Students");
}
}
}

EF上下文类:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Ef.Data
{
public class EFDbContext:DbContext
{
public EFDbContext()
: base("name=DbConnectionString")
{ } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new StudentMap());
//base.OnModelCreating(modelBuilder);
}
}
}

数据库连接字符串:【在EF.Data的配置文件和控制台的项目的配置文件中都要写:】另外注意:这两个项目需要引入EF Server. 代表本机
<connectionStrings>
<add name="DbConnectionString" connectionString="Server=.;database=StudentEFDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient" />
</connectionStrings>
connectionString=“Server=.;Database=AppsDB;Trusted_Connection=True” 也可以
控制台中:

using Ef.Data;
using EF.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EF.App
{
public class Program
{ static void Main(string[] args)
{
Console.WriteLine("你好,请输入姓名:");
string name = Console.ReadLine().Trim();
Console.WriteLine("请输入年龄:");
int result = 0;
//转换成功,result是正确的结果
if (!int.TryParse(Console.ReadLine().Trim(), out result))
{
Console.WriteLine("请输入正确格式的年龄");
return;
}
Console.WriteLine("请输入你的性别:");
string sex = Console.ReadLine(); using(var db=new EFDbContext())
{
Student model = new Student()
{
Name = name,
Sex = sex,
Age = result }; //db.Set<Student>().Add(model);或者下面的
db.Entry(model).State = System.Data.Entity.EntityState.Added;
db.SaveChanges(); }
Console.Write("Success!");
Console.ReadKey(); }
}
}

运行之后,写入数据。
看下数据库中的数据:
好了,这就完成了第一阶段的准备工作:现在我们需要在Student实体中加一个字段Email,项目做一下变动:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EF.Core
{
public class Student
{
public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } public string Email { get; set; }
}
}


using EF.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Ef.Data
{
public class StudentMap:EntityTypeConfiguration<Student>
{
public StudentMap()
{
this.HasKey(s => s.ID);
this.Property(s => s.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(s => s.Name).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();
this.Property(s => s.Sex).HasColumnType("nvarchar").IsRequired();
this.Property(s => s.Age).IsRequired();
this.Property(s => s.Email).IsRequired(); this.ToTable("Students");
}
}
}


using Ef.Data;
using EF.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EF.App
{
public class Program
{ static void Main(string[] args)
{
Console.WriteLine("你好,请输入姓名:");
string name = Console.ReadLine().Trim();
Console.WriteLine("请输入年龄:");
int result = 0;
//转换成功,result是正确的结果
if (!int.TryParse(Console.ReadLine().Trim(), out result))
{
Console.WriteLine("请输入正确格式的年龄");
return;
}
Console.WriteLine("请输入你的性别:");
string sex = Console.ReadLine(); Console.WriteLine("请输入你的Email:");
string email = Console.ReadLine(); using(var db=new EFDbContext())
{
Student model = new Student()
{
Name = name,
Sex = sex,
Age = result,
Email=email }; //db.Set<Student>().Add(model);或者下面的
db.Entry(model).State = System.Data.Entity.EntityState.Added;
db.SaveChanges(); }
Console.Write("Success!");
Console.ReadKey(); }
}
}

运行之后:报错,很正常嘛,这肯定报错,因为我们的实体改了。。
怎么办呢??别着急,我们来启用数据库迁移技术:
在程序包控制台中输入:
Enable-Migrations
接着按回车键,在项目中就会生成Migration文件夹,自己去看看里面有啥东东吧。
然后打开Configuration类:修改一下代码:(必须把DbMigrationsConfiguration的参数改成自己程序里的DbContext的namespace+实际类名)
接着在程序包管理器控制台中输入:
Update-Database -Verbose
好了,现在运行一下项目吧:
看到了么,到这里就没报错了。运行完之后,我们看下数据库中的数据:
可以看到之前的数据也在,也就是数据没有丢失。是不是很神奇呢???
总结:这两篇文章总结了EF的一些使用,希望你喜欢。
题外篇:这里是使用EF中的数据库迁移技术。我们就可以随时随地的添加删除字段。而不用手动去删除数据库,那样会丢失数据。
现在,我就想不用数据库迁移呢?
我删掉之前生成的Migration文件夹【包括里面的类】,然后修改代码【删除刚才添加的Email字段】:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EF.Core
{
public class Student
{
public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } // public string Email { get; set; }
}
}


using EF.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Ef.Data
{
public class StudentMap:EntityTypeConfiguration<Student>
{
public StudentMap()
{
this.HasKey(s => s.ID);
this.Property(s => s.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(s => s.Name).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();
this.Property(s => s.Sex).HasColumnType("nvarchar").IsRequired();
this.Property(s => s.Age).IsRequired();
// this.Property(s => s.Email).IsRequired(); this.ToTable("Students");
}
}
}


using Ef.Data;
using EF.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EF.App
{
public class Program
{ static void Main(string[] args)
{
Console.WriteLine("你好,请输入姓名:");
string name = Console.ReadLine().Trim();
Console.WriteLine("请输入年龄:");
int result = 0;
//转换成功,result是正确的结果
if (!int.TryParse(Console.ReadLine().Trim(), out result))
{
Console.WriteLine("请输入正确格式的年龄");
return;
}
Console.WriteLine("请输入你的性别:");
string sex = Console.ReadLine(); //Console.WriteLine("请输入你的Email:");
//string email = Console.ReadLine(); using(var db=new EFDbContext())
{
Student model = new Student()
{
Name = name,
Sex = sex,
Age = result,
// Email=email }; //db.Set<Student>().Add(model);或者下面的
db.Entry(model).State = System.Data.Entity.EntityState.Added;
db.SaveChanges(); }
Console.Write("Success!");
Console.ReadKey(); }
}
}

运行之后,还肯定会报错的,因为已经没有数据库迁移了:
我们做如下修改:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Ef.Data
{
public class EFDbContext:DbContext
{
public EFDbContext()
: base("name=DbConnectionString")
{
//把数据库初始化策略设置为null
Database.SetInitializer<EFDbContext>(null);
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new StudentMap());
//base.OnModelCreating(modelBuilder);
}
}
}

然后运行项目,就不错报错了,这是不使用数据库迁移技术做到的。
看下数据库:
两种方式都可以,不过还是推荐官方的方式,数据库迁移技术。
EF中 Code-First 方式的数据库迁移的更多相关文章
- 2.EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- EF 中 Code First 的数据迁移以及创建视图
写在前面: EF 中 Code First 的数据迁移网上有很多资料,我这份并没什么特别.Code First 创建视图网上也有很多资料,但好像很麻烦,而且亲测好像是无效的方法(可能是我太笨,没搞成功 ...
- MVC中code first方式开发,数据库的生成与更新
在使用EF的实际编程中我们经常遇到这样的问题:发现实体结构需要新增加一个字段,或者减少一个字段,急需把实体结构修改,并让数据库更新这种修改.在用Model First或者Database First的 ...
- EF Code First教程-03 数据库迁移Migrator
要在nuget 程序包管理控制台中输入命令 基本命令 Enable-Migrations //打开数据库迁移 Add-Migration AddBlogUrl //新增一个数据库迁移版本 ...
- 【EF6学习笔记】(一)Code First 方式生成数据库及初始化数据库实际操作
本篇参考原文地址: Creating an Entity Framework Data Model 说明:学习笔记参考原文中的流程,为了增加实际操作性,并能够深入理解,部分地方根据实际情况做了一些调整 ...
- EF6 学习笔记(一):Code First 方式生成数据库及初始化数据库实际操作
EF6 学习笔记总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 本篇参考原文地址: Creating an Entity Framework Data Model 说明:学习 ...
- EF架构~codeFirst从初始化到数据库迁移
一些介绍 CodeFirst是EntityFrameworks的一种开发模式,即代码优先,它以业务代码为主,通过代码来生成数据库,并且加上migration的强大数据表比对功能来生成数据库版本,让程序 ...
- 【EF Code First】Migrations数据库迁移
1,打开工具->NuGet程序管理器->程序包管理器控制台 默认项目中要选择 数据访问上下文类 所在的项目 我的DB是在命名空间CodeFirst.UI下的所以选择CodeFirst. ...
- 【转】MVC中code first方式开发,数据库的生成与更新(Ef6)
一,在models文件夹中,建立相应的model文件 这里注意一点,这里建立的class名,就是数据库里表的名字. 在这里面,可以建立表之间的关系. 这里要说明一点的事 ...
随机推荐
- x86保护模式-七中断和异常
x86保护模式-七中断和异常 386相比较之前的cpu 增强了中断处理能力 并且引入了 异常概念 一 80386的中断和异常 为了支持多任务和虚拟存储器等功能,386把外部中断称为中断 ...
- windows和ubuntu14.04双系统设置默认启动项
首先开机或者重启,在启动项选择菜单处记住win7对应的序号,从上至下的序号从0开始计数,我的win7系统选项处于第5个,那么序号就应该是4,记住后,打开ubuntu系统. 2 按下Ctrl+alt+T ...
- apache kafka参考
apache kafka参考 消息队列分类: 点对点: 消息生产者生产消息发送到queue中,然后消息消费者从queue中取出并且消费消息.这里要注意: 消息被消费以后,queue中不再有存储,所以 ...
- BZOJ 2005 [Noi2010]能量采集 ——Dirichlet积
[题目分析] 卷积一卷. 然后分块去一段一段的求. O(n)即可. [代码] #include <cstdio> #include <cstring> #include < ...
- POJ 2411 Mondriaan's Dream ——状压DP 插头DP
[题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...
- Redis的持久化——RDB
前面说到redis的三大特性:缓存.分布式内存数据库.持久化,所以今天将为大家介绍redis的两种数据持久化技术RDB和AOF, 先介绍RDB吧. 一.RDB是什么? 1.RDB全称redis dat ...
- P1857 质数取石子 (DP,递推)
题目描述 桌上有若干个石子,每次可以取质数个.谁先取不了,谁就输.问最少几步能赢?(一个人取一次算一步) 输入输出格式 输入格式: 第一行N,表示有N组数据 接下来N行为石子数 输出格式: 每组数据一 ...
- spring中quartz的使用。【转http://www.cnblogs.com/kay/archive/2007/11/02/947372.html】
注:从spring3到spring4改变 org.springframework.scheduling.quartz.CronTriggerBean org.springframework.sched ...
- Nova 组件详解
本节开始,我们将详细讲解 Nova 的各个子服务. 前面架构概览一节知道 Nova 有若干 nova-* 的子服务,下面我们将依次学习最重要的几个.今天先讨论 nova-api 和 nova-cond ...
- CKeditor如何实现图片上传功能
http://makaiyuan.blog.51cto.com/5819595/1049521 如何在数据库中导入excel文件内的数据:http://jingyan.baidu.com/album/ ...