Entity Framework Customizing the Migrations History Table (EF6 onwards)

Entity Framework自定义迁移历史表(EF6以上)

来源

EF6 Onwards Only - The features, APIs, etc. discussed in this page were introduced in Entity Framework 6. If you are using an earlier version, some or all of the information does not apply.

仅EF6以上-本文讨论的特征、API等在EF6中才被引入,如果你使用的是早期的版本,本文所涉及的部分或全部信息可能不适用。

1What is Migrations History Table?什么是迁移历史表

Migrations history table is a table used by Code First Migrations to store details about migrations applied to the database. By default the name of the table in the database is __MigrationHistory and it is created when applying the first migration do the database. In Entity Framework 5 this table was a system table if the application used Microsoft Sql Server database. This has changed in Entity Framework 6 however and the migrations history table is no longer marked a system table.

历史迁移表(Migrations history table)是代码优先迁移(Code First Migrations)用来存储有关施加到数据库的迁移细节的表,缺省地,数据库中该表的名称为__MigrationHistory,而且,当应用第一次迁移到数据库时被创建。在Entity Frame 5中,如果应用使用微软的sql server数据库,则该表作为一个系统表,然而,在Entity Frame 6中则不然,该迁移表不再被标记为系统表。

2Why customize Migrations History Table?为什么自定义迁移历史表

Migrations history table is supposed假定to be used solely[ˈsəʊlli唯一地]  by Code First Migrations and changing it manually can break migrations. However sometimes the default configuration is not suitable and the table needs to be customized, for instance:

l You need to change names and/or facets of the columns to enable a 3rd party Migrations provider

l You want to change the name of the table

l You need to use a non-default schema for the __MigrationHistory table

l You need to store additional data for a given version of the context and therefore you need to add an additional column to the table

迁移历史表被假定为仅仅被代码优先迁移使用,而且人为的改变它会中止迁移,然而,有些时候缺省的配置并不是适合的,而且这个表需要来进行自定义,比如:

l 你需要改变列的名称和(或)他方面来启用第三方迁移提供者。

l 你要改变该表的名称。

l 你需要为__MigrationHistory表使用一个非缺省计划。

l 你需要为一个上下文的确定版本存储附加的数据为此你需要增加一个附加的列到表中。

3Words of precaution警告

Changing the migration history table is powerful but you need to be careful to not overdo it. EF runtime currently does not check whether the customized migrations history table is compatible [kəmˈpætəbl]  with the runtime. If it is not your application may break at runtime or behave in unpredictable [ˌʌnprɪˈdɪktəbl] ways. This is even more important if you use multiple contexts per database in which case multiple contexts can use the same migration history table to store information about migrations.

改变迁移表是有效的,但你需要小心不要太过火,当前EF运行时不检查自定义的迁移历史表是否与运行时兼容。如果不兼容,应用则可能会在运行中停止或出现不可预料的情况。当你使用多上下文每数据库,在这种情况下多上下文可能使用相同的迁移历史表来存储有关迁移的信息时,这甚至会更重要。

(注:我们知道上下文为连接字符串有关和表有关,而连接字符串只与数据库有关。)

4How to customize Migrations History Table?如何自定义迁移历史表

Before you start you need to know that you can customize the migrations history table only before you apply the first migration. Now, to the code.

First, you will need to create a class derived from System.Data.Entity.Migrations.History.HistoryContext class. The HistoryContext class is derived from the DbContext class so configuring the migrations history table is very similar to configuring EF models with fluent[ˈflu:ənt]  API. You just need to override the OnModelCreating method and use fluent API to configure the table.

开始之前,你需要知道,你只能在应用首次迁移之前可以自定义迁移历史表,现在,对于代码,首先,你需要创建一个继承于System.Data.Entity.Migrations.History.HistoryContext的类,该HistoryContext类继承于DbContext类,这样,配置迁移历史表非常类似于用流畅的API配置EF模型,你只需要重写OnModelCreating方法并使用流畅的API来配置表。

Note: Typically when you configure EF models you don’t need to call base.OnModelCreating() from the overridden OnModelCreating method since the DbContext.OnModelCreating() has empty body. This is not the case when configuring the migrations history table. In this case the first thing to do in your OnModelCreating() override is to actually call base.OnModelCreating(). This will configure the migrations history table in the default way which you then tweak in the overriding method.

注意:典型地,当你配置EF模型时,你不需从已重写的OnModelCreating method来调用base.OnModelCreating(),因为DbContext.OnModelCreating()是一个空方法。但配置迁移历史表则不然,你首先要在重写的OnModelCreating()方法中做的事就是调用base.OnModelCreating()。这将会把迁移历史表配置为缺省的方式,然后你可以在重写的方法中进行微调。

Let’s say you want to rename the migrations history table and put it to a custom schema called“admin”. In addition your DBA would like you to rename the MigrationId column to Migration_ID. You could achieve this by creating the following class derived from HistoryContext:

当你想重命名该迁移历史表并它放到称为“Admin”的计划中。另外,你的数据管理员可能希望你将MigrationId列重命名为Migraiton_ID,通过创建以下继承自HistoryContext的类就可实现这些。

using System.Data.Common;

using System.Data.Entity;

using System.Data.Entity.Migrations.History;

namespace CustomizableMigrationsHistoryTableSample

{

public class MyHistoryContext : HistoryContext

{

public MyHistoryContext(DbConnection dbConnection, string defaultSchema)

: base(dbConnection, defaultSchema)

{

}

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder);

modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "admin");

modelBuilder.Entity<HistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID");

}

}

}

Once your custom HistoryContext is ready you need to make EF aware of it by registering it via  code-based configuration:

一旦你的自定义HistoryContext准备就绪,你需要通过用基于代码的配置注册它使EF知道它:

using System.Data.Entity;

namespace CustomizableMigrationsHistoryTableSample

{

public class ModelConfiguration : DbConfiguration

{

public ModelConfiguration()

{

this.SetHistoryContext("System.Data.SqlClient",

(connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema));

}

}

}

That’s pretty much it. Now you can go to the Package Manager Console, Enable-Migrations, Add-Migration and finally Update-Database. This should result in adding to the database a migrations history table configured according to the details you specified in your HistoryContext derived class.

差不多了,现在你可以到包管理控制台,Enable-Migrations,Add-Migration并最终Update-Database。应该会导致增加一个根据你在HistoryContext继承类中指定的细节配置的迁移历史表到数据库中了。

Entity Framework自定义迁移历史表(EF6以上)相关推荐

  1. Entity Framework如何得到数据库表的名字

    在Entity Framework中,有时我们想要得到真正的数据库表的名称,可以通过Metadata来得到. 直接上代码: View Code 1 static void Main(string[] ...

  2. Entity framework 意外删除了表,如何在不影响其它表的情况下恢复回来 (EF数据库迁移原理)...

     关于EntityFramework数据迁移原理 查询数据库的表"__MigrationHistory",遍历代码库的Migrations文件夹下的所有文件,如果文件不在__Mig ...

  3. (解题思路)Entity Framework 如动态创建表或者列

    1.数据库中建立关于表结构信息的视图.(这个完全可以做到) 2.根据视图信息动态生成(内存.文件)edmx信息,根据edmx信息动态生成ObjectContext内容. 3.动态编译edmx及Obje ...

  4. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

  5. Entity Framework Core 命名约定

    注意:我使用的是 Entity Framework Core 2.0 (2.0.0-preview2-final).正式版发布时,功能可能存在变动.Entity Framework 迁移允许从模型生成 ...

  6. Entity Framework Core 之数据库迁移

    前言 最近打算用.NET Core写一份开源的简易CMS系统,来练练手 所以又去深入研究了一下Entity Framework Core 发现其实有些细节园子里还是很少讲到. 特意整理了几个细节. 正 ...

  7. abp mysql .net core_ABP .Net Core Entity Framework迁移使用MySql数据库

    一.迁移说明 ABP模板项目Entity Framework Core默认使用的是Sql Server,也很容易将数据库迁移到MySQL,步骤如下. 二.迁移MySQL步骤 1. 下载项目 请到 ht ...

  8. ASP.NET MVC5 + EF6 入门教程 (5) Model和Entity Framework

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-ef-6-get-started-model.html 上一节:ASP.NET MVC ...

  9. Entity Framework 6新特性:全局性地自定义Code First约定

    2012年12月11日,Entity Framework已经发布了Entity Framework 6 Alpha2,因项目需要,目前已使用了其中的两个特性,今天就来介绍一下第一个特性:全局性地自定义 ...

最新文章

  1. OpenCV GPU 简单遍历图像
  2. SQL Server 2012高可用性组
  3. word精华样式篇之三使用样式集让所有文档风格统一
  4. mysql之case_mysql存储过程之case语句
  5. 关于JavaScript的变量和函数提升
  6. Dapr 运用之集成 Asp.Net Core Grpc 调用篇
  7. php html标签自定义属性,详解H5的自定义属性data-*
  8. 【Java中级篇】动态代理机制
  9. 如何在JS判断是否为IE浏览器
  10. vulkan api_Vulkan开放标准API支持,针对Linux的新游戏以及更多游戏
  11. iOS换肤功能的简单处理框架
  12. 光纤传输与网络技术作业(第一部分)
  13. Oracle PL/SQL游标的学习
  14. 《大型网站技术架构:核心原理与案例分析》读书笔记-高可用
  15. java实训致谢_Java教学实习报告(最终版).doc
  16. java反序加密_对java程序加密防止反编译
  17. 如何从手机上恢复误删的微信聊天记录
  18. win7原版镜像_小白重装win7旗舰版系统图文教程
  19. blender bpy
  20. 原力计划S5上榜博主名单公布(第四期已更新)

热门文章

  1. python实现SCP文件上传服务器
  2. 什么是模型?开发软件为什么要建模?
  3. 为什么越长大越不想过生日
  4. 中国企业学习管理系统(LMS)市场趋势报告、技术动态创新及市场预测
  5. 用华为手机现在还不知道这5种实用功能,几千块白花了,太浪费了
  6. lms全称是什么意思_lms是什么意思?
  7. win7 x64下安装python-opencv 及 “not a supported wheel”解决
  8. linux进程创建心得体会,Linux系统下计算机C语言编程心得体会
  9. python模块化导入
  10. win7查找计算机在哪里,win7搜索在哪