原文链接:https://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx

EF 6 Code-First系列文章目录:

Entity Framework介绍了自动迁移技术,所以每次实体发生改变的时候,你不用手动去处理数据库迁移。

自动迁移技术可以通过在程序包管理控制台中输入并执行:enable-migrations命令做到。打开程序包管理控制台,输入:enable-migrations –EnableAutomaticMigration:$true【确保默认的项目是你现在要执行的项目】

当命令执行成功之后,将会创建一个internal sealed Configuration 类,这个Configuration类继承自DbMigrationConfiguration :

正如你在COnfiguration类的构造函数中看到的那样,AutomaticMigrationsEnabled 被设置为true.

下一步,就是在上下文类中设置数据库初始化策略为MigrateDatabaseToLatestVersion:

public class SchoolContext: DbContext
{
public SchoolDBContext(): base("SchoolDB")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolDBContext, EF6Console.Migrations.Configuration>());
} public DbSet<Student> Students { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}

现在你就完成了自动化迁移技术的配置。当实体发生改变的时候,EF将会自动进行数据库迁移。目前为止,我们只有一个Student实体,还有一个上下文类,我们运行项目看看生成的数据库:

你将会发现EF API创建了__MigrationHistory 表和Students表。__MigrationHistory 包含了每次数据库迁移的历史记录。

现在,你添加一个新的领域类实体,运行程序,会发现数据库自动包含了所有实体所映射的表。你不用运行任何其他命令。

然而,这样只是针对添加实体或者移除实体才有用,当你向实体中添加、修改或者删除属性的时候,并不起作用。删除领域类的任何一个属性,运行项目:

这样是因为你将会丢失相应列的数据。为了解决这个,你需要在Configuration类的构造函数中,设置AutomaticMigrationDataLossAllowed 为true,并且设置AutomaticMigrationsEnabled = true。

为了了解更多enable-migrations命令的参数,可以执行get-help enable-migrations 或者get-help enable-migrations -detailed,你将会看到:

PM> get-help enable-migrations

NAME
Enable-Migrations SYNOPSIS
Enables Code First Migrations in a project. SYNTAX
Enable-Migrations [-ContextTypeName <String>] [-EnableAutomaticMigrations]
[-MigrationsDirectory <String>] [-ProjectName <String>] [-StartUpProjectName
<String>] [-ContextProjectName <String>] [-ConnectionStringName <String>]
[-Force] [-ContextAssemblyName <String>] [-AppDomainBaseDirectory <String>]
[<CommonParameters>] Enable-Migrations [-ContextTypeName <String>] [-EnableAutomaticMigrations]
[-MigrationsDirectory <String>] [-ProjectName <String>] [-StartUpProjectName
<String>] [-ContextProjectName <String>] -ConnectionString <String>
-ConnectionProviderName <String> [-Force] [-ContextAssemblyName <String>]
[-AppDomainBaseDirectory <String>] [<CommonParameters>] DESCRIPTION
Enables Migrations by scaffolding a migrations configuration class in the project. If the
target database was created by an initializer, an initial migration will be created (unless
automatic migrations are enabled via the EnableAutomaticMigrations parameter). RELATED LINKS REMARKS
To see the examples, type: "get-help Enable-Migrations -examples".
For more information, type: "get-help Enable-Migrations -detailed".
For technical information, type: "get-help Enable-Migrations -full".

20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】的更多相关文章

  1. 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx EF 6 Code-First ...

  2. 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx EF 6 Code- ...

  3. 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/database-initialization-in-code-first.aspx EF ...

  4. 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx EF 6 ...

  5. 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...

  6. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  7. 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-f ...

  8. 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.a ...

  9. 文章翻译:ABP如何在EF core中添加数据过滤器

    原文地址:https://aspnetboilerplate.com/Pages/Documents/Articles%5CHow-To%5Cadd-custom-data-filter-ef-cor ...

随机推荐

  1. c++ <vector>学习

    https://www.cnblogs.com/mr-wid/archive/2013/01/22/2871105.html 具体参考上面博客,很详细,也是看他的.今天c++学习200%,项目开发0% ...

  2. Web Service 消息格式

    当客户端和Web Service服务器进行通信时,他们交换消息.客户端发送请求消息到Web Service服务器. Web Service服务器响应并返回消息.这就像普通的HTTP,浏览器发送一个HT ...

  3. Windows 账户名及机器名相关问题记录

    问题 1 背景:因为部门调动,需要接手同事的 PC.相关工具配置好之后,通过"控制面板->用户帐户和家庭安全->用户帐户->管理帐户->更改帐户->重命名帐户& ...

  4. mongodb副本集搭建

    1.创建目录 mkdir -p /data/r1  /data/r2  /data/r3 2.启动: bin/mongod --config ../mongod.conf --replSet r1 b ...

  5. BZOJ2702 : 金融风暴

    求出离每个点最近的关键点,然后用二维ST表回答正方形最大值. 将关键点分为上下两部分,以上为例: 从上到下依次考虑每一行,记录每一列往上最近的关键点的距离,那么最优决策具有单调性,可以分治求解. 时间 ...

  6. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习9

    #include <iostream>#include <string>using namespace std;struct CandyBar{ //string kind;  ...

  7. HTTP1.1协议-RFC2616-中文版课前资料收集

    1.http rfc大致讲了什么? 2.解决了什么问题? HTTP协议描述的是发送方与接收方的通信协议 协议功能: HTTP协议(HyperText Transfer Protocol,超文本传输协议 ...

  8. parcel 中小型项目打包工具

    "0配置"打包器(bundler)Parcel Parcel官网(有中文文档) webpack 要有大量的配置,这样带来的成本就是复杂性--与此相对的,Parcel 带来了简洁性. ...

  9. Spark 1.x 爆内存相关问题汇总及解

    Spark 1.x 爆内存相关问题汇总及解决 OOM # 包括GC Overhead limitjava.lang.OutOfMemoryError # on yarn org.apache.hado ...

  10. 牛客网--C++-2017/8/19

    “\t\v\\0”长度=4:\0:字符串结束符:\\0:\将\0进行了转义,所以\0是两个字符 类的友元函数的访问权限跟类内部的方法相同,但是友元函数不属于本类的对象,一般它是另一个类的成员函数,不能 ...