原文链接: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. burpsuite https证书设置

    java更新.burpsuite换来换去,chrome的证书似乎失效了.重新来一边证书导入,有一些导入方法确实坑. 尝试了直接导入到受信任的机构是无效的. 两年前就因为导入到受信任的机构,又找不到导入 ...

  2. python数据结构之插入排序

    插入排序(英语:Insertion Sort)是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,在从后向前扫描 ...

  3. elasticsearch数据备份还原

    elasticsearch数据备份还原 1.在浏览器中运行http://XXX.XXX.XXX.XXX:9200/_flush,确保索引数据能保存到硬盘中. 2.原数据的备份.主要是elasticse ...

  4. vue注册和简单使用

      组件的出现就是为了解决页面布局等等一些列的问题. vue中的组件分为两种,全局组件和局部组件. 一  . 注册全局组件 通过Vue.component()创建一个全局组件之后,我们可以在一个通过  ...

  5. C#最简单的连接数据库的方法

    在vs2010下建立项目(可以是WEB或者是FORM窗体应用程序),在VS2010中,找到“服务器资源管理器”,右击“数据连接”.在添加连接中设置服务器名(登录SQL Server时的服务器名称,可以 ...

  6. window下安装PIL

    PIL非官方库64 友情连接: https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ ...

  7. Python——运算符

    Python算术运算符 以下假设变量: a=10,b=20: 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 ...

  8. Android Studio 2.3 解决小米手机调试安装apk失败问题

    在开发者选项里面,拉到底,把miui优化选项去掉就好了. 参考资料 [问题反馈] Android Studio 2.3 在红米note3手机中 调试安装Apk失败

  9. SpringMVC Controller之间的重定向和转发

    同一个controller之间重定向和转发 ①redirect 在Controller的映射方法中,其返回值改为:return "redirect:XXX"; ②forward 这 ...

  10. MySql的优化步骤

    MySql优化的一般步骤: 1.通过show status 命令了解各种sql的执行效率 SHOW STATUS提供msyql服务器的状态信息 一般情况下,我们只需要了解以”Com”开头的指令 sho ...