EF CodeFirs 代码迁移、数据迁移

最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。

十年河东十年河西,莫欺少年穷

学无止境,精益求精

   标题叫EF CodeFirs 代码迁移、数据迁移。

那么:到底叫代码迁移还是数据迁移?我在网上看了大半天,怎么叫的都有,后来查了MSDN,MSDN上叫代码迁移。在此,我们也称之为代码迁移。

为什么有人将其称为数据迁移呢?可能是因为本节内容和操作数据库有关<增加一张表,删除一张表,增加一个表字段,删除一个表字段,修改一个表字段等>,所以网友称之为数据迁移

MSDN等权威结构为什么称之为代码迁移呢?可能是因为本节的内容通过我们熟知的C#代码就可以搞定,所以称之为代码迁移

嘻嘻,上边是我的猜测,人家可跳过

正文:

背景:我们为什么要使用代码迁移?

我们使用CodeFirst初始化数据库无非以下三种方式:

一、 每次运行都会创建新的数据库

Database.SetInitializer<XXXXXContext>(new DropCreateDatabaseAlways<XXXXXContext>());

二、只有第一次运行~才会创建新的数据库~默认的方式

Database.SetInitializer<XXXXXContext>(new CreateDatabaseIfNotExists<XXXXXContext>());

三、 修改模型后~运行~会创建新的数据库

Database.SetInitializer<XXXXXContext>(new DropCreateDatabaseIfModelChanges<XXXXXContext>());

不论我们使用哪种方式,当条件成立时,都会创建新的数据库,而创建新的数据库就意味着‘从头再来’

举个例子:如果您的团队开发的项目已经被客户使用,客户在实际的生产操作中已经创建了很多数据,如果,某一天客户更改需求,我们难不成要采用上述方式将数据库重建?一旦重建,客户之前的数据就会丢失,这是任何一个客户不能容忍的

为了解决这个问题,我们要引入代码迁移

在进行代码迁移之前,我们必须做好数据库备份工作,确保客户数据的安全性及完整性

现在我们引入数据迁移示例:

我们有以下模型类:

Student模型类

    public class Student
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(10)]
        public string Name { get; set; }//姓名
        [StringLength(2)]
        public string Sex { get; set; }//性别
        [Unique(ErrorMessage="学号不允许重复")]
        [StringLength(18)]
        public string StudentNum { get; set; }//学号

        [StringLength(100)]
        public string StudentAddress { get; set; }//地址
    }

Course模型类

    public class Course
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(20)]
        public string Name { get; set; }//课程名称     }

Score模型类

        [Key]
        public int Id { get; set; }

        public int StudentScore { get; set; }//学生分数

        public int StudentID { get; set; }//学生ID

        public int CourseID { get; set; }//课程ID

        public virtual Student Student { get; set; }//virtual关键字修饰,用于延迟加载 提高性能 只有显式调用时 才会加载 并可以代表一个Student对象 也就是 属性==对象

        public virtual Course Course { get; set; }//virtual关键字修饰,用于延迟加载 提高性能 只有显式调用时  才会加载 并可以代表一个Course对象 也就是 属性==对象

StudentContext上下文

    public class StudentContext : DbContext
    {
        public StudentContext()
            : base("StudentContext")//指定连接字符串
        {

        }
        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Score> Scores { get; set; }
        /// <summary>
        /// OnModelCreating方法中的modelBuilder.Conventions.Remove语句禁止表名称正在多元化。如果你不这样做,所生成的表将命名为Students、Courses和Enrollments。相反,表名称将是Student、Course和Enrollment。开发商不同意关于表名称应该多数。本教程使用的是单数形式,但重要的一点是,您可以选择哪个你更喜欢通过包括或省略这行代码的形式。
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

现在客户提出如下需求:在确保数据完整性的情况下,添加一张学校表<字段:学校名称、学校地址,学校热线>及增加学生联系方式字段

首先,我们添加学校模型类:

    public class School//新建一张学校表-----------------------------新建的表
    {
        [Key]
        public int schoolId { get; set; }
        [Required]
        [StringLength(10)]
        public string schoolName { get; set; }//学校名称

        [StringLength(100)]
        public string StudentAddress { get; set; }//学校地址

        [StringLength(20)]
        public string StudentTel { get; set; }//学校热线
    }

修改学生模型类如下:

    public class Student
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(10)]
        public string Name { get; set; }//姓名
        [StringLength(2)]
        public string Sex { get; set; }//性别
        [Unique(ErrorMessage="学号不允许重复")]
        [StringLength(18)]
        public string StudentNum { get; set; }//学号

        [StringLength(100)]
        public string StudentAddress { get; set; }//地址

        [StringLength(11)]
        public string StudentTel { get; set; }//联系电话----------------------------新加的字段
    }

上下文类加上DBset<School>如下:

    public class StudentContext : DbContext
    {
        public StudentContext()
            : base("StudentContext")//指定连接字符串
        {

        }
        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Score> Scores { get; set; }
        public DbSet<School> Schools { get; set; }//新加一张表
        /// <summary>
        /// OnModelCreating方法中的modelBuilder.Conventions.Remove语句禁止表名称正在多元化。如果你不这样做,所生成的表将命名为Students、Courses和Enrollments。相反,表名称将是Student、Course和Enrollment。开发商不同意关于表名称应该多数。本教程使用的是单数形式,但重要的一点是,您可以选择哪个你更喜欢通过包括或省略这行代码的形式。
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

下面,我们进行代码迁移<在进行代码迁移之前,您必须重新生成您的项目>:

首先,打开程序包管理器控制台

PM>提示符下输入以下命令︰

 enable-migrations

 add-migration InitialCreate

解释下这两个命令的含义:enable-migrations 是指启动迁移,此命令输入后,系统会检查上下文目标  add-migration InitialCreate 创建名称为:时间戳_InitialCreate.CS的文件,其中时间戳是根据当前时间点生成的。

我们得到如下信息

至此,我们在项目中会发现多出一个名为:Migrations的文件夹,文件夹内有两个文件:201612080727320_InitialCreate.cs 和 Configuration.cs

下面介绍下这两个文件的作用:

Configuration.cs 中有个Seed方法,您每次代码迁移执行时,都会执行seed()方法,您可以通过Seed方法增加一些必要的数据。例如,上述我们增加了一张学校模型类,在代码迁移时,我们希望插入一条学校数据,我们可以这样修改seed()方法:

下面是我修改后的Seed方法:

namespace EF_Test.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using EF_Test.DAL;

    internal sealed class Configuration : DbMigrationsConfiguration<EF_Test.DAL.StudentContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(EF_Test.DAL.StudentContext context)
        {
            context.Schools.AddOrUpdate(
                 p => p.schoolId,
                 new School { schoolName = "河南城建学院", StudentAddress = "平顶山市新华区" , StudentTel="0375-88888888" }
               );
            context.SaveChanges();
        }
    }
}

201612080727320_InitialCreate.cs 或者称为:时间戳_InitialCreate.cs 是干嘛用的呢?我们来看看生成的代码,如下:

namespace EF_Test.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Course",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 20),
                    })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Score",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        StudentScore = c.Int(nullable: false),
                        StudentID = c.Int(nullable: false),
                        CourseID = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Course", t => t.CourseID, cascadeDelete: true)
                .ForeignKey("dbo.Student", t => t.StudentID, cascadeDelete: true)
                .Index(t => t.StudentID)
                .Index(t => t.CourseID);

            CreateTable(
                "dbo.Student",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 10),
                        Sex = c.String(maxLength: 2),
                        StudentNum = c.String(maxLength: 18),
                        StudentAddress = c.String(maxLength: 100),
                        StudentTel = c.String(maxLength: 11),
                    })
                .PrimaryKey(t => t.Id);

        }

        public override void Down()
        {
            DropForeignKey("dbo.Score", "StudentID", "dbo.Student");
            DropForeignKey("dbo.Score", "CourseID", "dbo.Course");
            DropIndex("dbo.Score", new[] { "CourseID" });
            DropIndex("dbo.Score", new[] { "StudentID" });
            DropTable("dbo.Student");
            DropTable("dbo.Score");
            DropTable("dbo.Course");
        }
    }
}

有两个方法,Up()和Down() 都是些创建表,删除主键,删除表的一些动作。我们先不管这些,继续我们的代码迁移:

PM>提示符下输入以下命令︰

   Update-Database  或者  Update-Database -Verbose   前者仅仅是更新数据库,不会在程序包管理器控制台中输出执行的SQL语句,而后者则可以输出执行的SQL语句,我们姑且使用后者吧

执行失败了,原因也很明确,是因为我们现有的数据库中已经存在Course等表,再次创建会发生异常,怎么办?

我们翻开 201612080727320_InitialCreate.cs 这个文件,将此文件作如下修改:

        public override void Up()
        {

            CreateTable(
                "dbo.School",
                c => new
                    {
                        schoolId = c.Int(nullable: false, identity: true),
                        schoolName = c.String(nullable: false, maxLength: 10),
                        StudentAddress = c.String(maxLength: 100),
                        StudentTel = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.schoolId);

        }

        public override void Down()
        {
            DropTable("dbo.School");

        }

项目生成成功后,再次执行:Update-Database -Verbose

   

万事大吉,执行成功,而且执行了Seed方法,那么我们的数据库中应该有一张名为 School 的表,并且里面有一条数据,如下:

学校模型创建成功了,但是,学生表中添加的字段加上去了么?

根据上图,学生表结构并没有加上StudentTel一列,为何?我们该怎么办?

在此,我们执行:add-migration 命令   add-migration 命令和 add-migration InitialCreate 命令的不同是一个指定了后缀名称,一个没有指定,需要我们自定义!

根据上图,我们创建了一个后缀为:SshoolCreate的文件,如下:

双击文件,我们发现里面没什么内容,我们添加如下代码:

    public partial class SshoolCreate : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Student", "StudentTel", c => c.String(maxLength: 11));
        }

        public override void Down()
        {
            DropColumn("dbo.Student", "StudentTel");
        }
    }

继续执行:update-database 命令

执行成功,我们到数据库看看

字段StudentTel已经加入了表Student

至此,EF 代码迁移也就讲完了,上文比较乱,最后做个总结:

1、 数据库中有张名为:__MigrationHistory的表,主要用于代码迁移记录

每当你成功迁移一次,都会生成一条记录。

2、上文中提到:时间戳_InitialCreate.CS 文件,如果在项目中有多个同类文件,项目会执行距离当前时间最近的 时间戳_InitialCreate.CS,如果距离当前时间最近的 时间戳_InitialCreate.CS 在表__MigrationHistory中有记录,则不会执行!

3、其实代码迁移用到的命令也就那几个,在此总结下:

enable-migrations : 启动代码迁移并生成Configuration.CS文件

add-migration : 启动生成 时间戳_Name.CS文件,类似于:201612080802330_Name.CS这种文件,需要指定Name,用于多次迁移

add-migration InitialCreate  : 启动生成类似于:201612080802330_InitialCreate.CS这种文件

Update-Database -Verbose  和  Update-Database 执行代码迁移,修改数据库表结构。 前者会在执行过程中生成相应的SQL语句

4、常用的C#代码:

创建一张表:

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.School",
                c => new
                    {
                        schoolId = c.Int(nullable: false, identity: true),
                        schoolName = c.String(nullable: false, maxLength: 10),
                        StudentAddress = c.String(maxLength: 100),
                        StudentTel = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.schoolId);
        }

        public override void Down()
        {
            DropTable("dbo.School");

        }

删除一张表:

    public partial class DropTable : DbMigration
    {
        public override void Up()
        {
            DropTable("dbo.School");
        }

        public override void Down()
        {
            CreateTable(
                "dbo.School",
                c => new
                    {
                        schoolId = c.Int(nullable: false, identity: true),
                        schoolName = c.String(nullable: false, maxLength: 10),
                        StudentAddress = c.String(maxLength: 100),
                        StudentTel = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.schoolId);

        }
    }

修改字段长度或字段名称

    public partial class EEE : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Student", "StudentPhone", c => c.String(maxLength: 11));
            DropColumn("dbo.Student", "StudentTel");
        }

        public override void Down()
        {
            AddColumn("dbo.Student", "StudentTel", c => c.String(maxLength: 11));
            DropColumn("dbo.Student", "StudentPhone");
        }
    }

增加表字段:

    public partial class FFF : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Student", "StudentPhowwwwne", c => c.String(maxLength: 11));
        }

        public override void Down()
        {
            DropColumn("dbo.Student", "StudentPhowwwwne");
        }
    }

删除表字段:

    public partial class GGG : DbMigration
    {
        public override void Up()
        {
            DropColumn("dbo.Student", "StudentPhowwwwne");
        }

        public override void Down()
        {
            AddColumn("dbo.Student", "StudentPhowwwwne", c => c.String(maxLength: 11));
        }
    }

等等吧,上述代码都能生成。

5、如果您生成了一个新的 时间戳_XXXX.CS文件,请务必使用Update-DataBase命令执行,否则,您将永远不会生成下一个 时间戳_XXXX.CS文件,究其原因,是因为在数据表[__MigrationHistory]中没有记录。

最新的[__MigrationHistory]数据

根据上述数据,就证明我做了五次代码迁移了!

好了,本节的内容也就讲完了,谢谢大家的耐心查阅!

@陈卧龙的博客

时间: 2024-08-24 04:37:04

EF CodeFirs 代码迁移、数据迁移的相关文章

20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】

原文链接:https://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx EF 6 Code-First系列文章目录: 1 翻译系列:什么是Code First(EF 6 Code First 系列) 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列) 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列) 4

EF Code First之数据迁移

1. Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDBContext 2. add-migration Initial //Initial”是随意命名的,它用来命名创建好的迁移文件. 3. update-database EF Code First之数据迁移

EF 中 Code First 的数据迁移以及创建视图

写在前面: EF 中 Code First 的数据迁移网上有很多资料,我这份并没什么特别.Code First 创建视图网上也有很多资料,但好像很麻烦,而且亲测好像是无效的方法(可能是我太笨,没搞成功),我摸索出了一种简单有效的方法,这里分享给大家. EF是Entity Framework(实体框架)的简写,是微软出品的用来操作数据库的一个框架,会ASP.NET MVC的朋友对他肯定都不陌生.由于学艺不精,我对EF存在一疑虑,就不以[提问]的方式来问了,我以[总结]的方式来表达,如果总结有误的地

【EF】EF Code-First数据迁移

Code-First数据迁移  首先要通过NuGet将EF升级至最新版本. 新建MVC 4项目MvcMigrationDemo 添加数据模型 Person 和 Department,定义如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.ComponentModel.DataAnnotations; 6 using Syste

EF Code-First数据迁移

Code-First数据迁移  首先要通过NuGet将EF升级至最新版本. 新建MVC 4项目MvcMigrationDemo 添加数据模型 Person 和 Department,定义如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.ComponentModel.DataAnnotations; 6 using Syste

EF架构~CodeFirst数据迁移与防数据库删除

回到目录 本文介绍两个概念,防数据库自动删除,这是由于在code first模式下,当数据实体发生变化时,会对原来数据库进行删除,并将新数据表添加进来,但这对于我们的运营环境数据库,是万万不能接受的,第二个问题是数据迁移问题,当你有新的实体建立后,如何响应到数据库,这成为一个问题,当然实现也很简单,我们直接使用migrations工具即可. 一 防数据库删除 将你的业务DbInitializer的基类改成CreateDatabaseIfNotExists即可解决这个问题,这是在数据初始化时需要做

iOS Core Data 数据迁移 指南

前言 Core Data是iOS上一个效率比较高的数据库框架,(但是Core Data并不是一种数据库,它底层还是利用Sqlite3来存储数据的),它可以把数据当成对象来操作,而且开发者并不需要在乎数据在磁盘上面的存储方式.它会把位于NSManagedObject Context里面的托管对象NSManagedObject类的实例或者某个NSManagedObject子类的实例,通过NSManagedObjectModel托管对象模型,把托管对象保存到持久化存储协调器NSPersistentSt

EF Code First:实体映射,数据迁移,重构(1)

一.前言 经过EF的<第一篇>,我们已经把数据访问层基本搭建起来了,但并没有涉及实体关系.实体关系对于一个数据库系统来说至关重要,而且EF的各个实体之间的联系,实体之间的协作,联合查询等也都依赖于这些实体关系. 二.实体映射 实体与数据库的映射可以通过DataAnnotation与FluentAPI两种方式来进行映射: (一) DataAnnotation DataAnnotation 特性由.NET 3.5中引进,给.NET中的类提供了一种添加验证的方式.DataAnnotation由命名空

EF 数据迁移

问 题 支持“AppContext”上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库 原因以及解决办法 在用EF CodeFirst模式开发当中难免会对类进行修改,修改后再次运行就会出现异常,提示上下文的模型已在数据库创建后发生改变. 如果是项目初期开发倒是好办,可以删掉数据库重新建立,但是如果项目已经运行了,我想你不会想初期那样做吧,会照成数据丢失的.这个时候就需要使用EF的数据迁移功能了,下面教你如何使用数据迁移. 打开VS,工具>库程序包管理器>程序