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

来源:互联网 发布:淘宝上钱包店铺哪家好 编辑:程序博客网 时间:2024/06/03 05:22

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 dont 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()。这将会把迁移历史表配置为缺省的方式,然后你可以在重写的方法中进行微调。

 

 

Lets say you want to rename the migrations history table and put it to a custom schema calledadmin. 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));

            }

        }

    }

 

 

Thats 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-MigrationsAdd-Migration并最终Update-Database。应该会导致增加一个根据你在HistoryContext继承类中指定的细节配置的迁移历史表到数据库中了。

 

0 0
原创粉丝点击