我如何附加列添加到__MigrationHistory表? [英] How do I add an additional column to the __MigrationHistory table?

查看:614
本文介绍了我如何附加列添加到__MigrationHistory表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义的迁移历史表,我应该能够添加列,但是我无法找到如何实际添加新列的例子。

Per Customizing the Migrations History Table, I should be able to add a column, however I'm not able to find any examples on how to actually add the new column.

我主要困惑在哪里把实际属性以及如何将其配置到现有的__MigrationHistory表。从文档,我可以自定义,像这样的表配置..

I'm mostly confused about where to put the actual property and how to configure it to the existing __MigrationHistory table. From the documentation, I can customize the table configuration like so..

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");
}



...但我不能修改 HistoryRow 实体。

我应该添加一个基于 HistoryRow 实体?

Am I supposed to add a new derived type based on the HistoryRow entity?

推荐答案

首先,你需要创建一个新类为您排历史的实体。例如:

First you need to create a new class for your history row entity. For example:

public sealed class MyHistoryRow : HistoryRow
{
    //We will just add a text column
    public string MyColumn { get; set; }
}



接下来我们需要一个背景下,同样在我们为EF正常运营做,

Next we need a context, same as we do for normal EF operations, this time however we inherit from HistoryContext:

public class MyHistoryContext : HistoryContext
{
    //We have to 'new' this as we are overriding the DbSet type
    public new DbSet<MyHistoryRow> History { get; set; }

    public MyHistoryContext(DbConnection dbConnection, string defaultSchema)
        : base(dbConnection, defaultSchema)
    {
    }

    //This part isn't needed but shows what you can do
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //Rename the table and put it in a different schema. Our new table
        //will be called 'admin.MigrationHistory'
        modelBuilder.Entity<MyHistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "admin");

        //Rename one of the columns for fun
        modelBuilder.Entity<MyHistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID");
    }
} 

现在来将它们连接起来,我们需要对它进行配置,所以首先我们设置的配置:

Now to wire them up, we need to configure it, so first we set up the configuration:

public class MyConfiguration : DbConfiguration
{
    public MyConfiguration()
    {
        //Set our new history context to be the one that gets used
        this.SetHistoryContext("System.Data.SqlClient",
            (connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema)); 
    }
}



最后,让配置应用通过修改我们的网站.config文件:(必须填写自己的名称空间和应用程序集:

And finally, make the configuration apply by modifying our web.config: (you will have to fill in your own namespace and application assembly:

<entityFramework codeConfigurationType="Namespace.MyConfiguration, ApplicationAssembly">
    ...snipped...
</entityFramework>

这样做的一个副作用是,如果/当您启用迁移,需要明确说明您正在使用的情况下,这显然是因为你现在在你的装配两种上下文类型(除非你拆出来。)所以你现在需要运行这样的命令:

A side effect of doing this is that if/when you enable migrations, you need to explicitly state the context you are working with. This is obviously because you now have two context types in your assembly (unless you split them out.) So you now need to run a command like this:

enable-migrations -ContextTypeName Namespace.YourContextClass

这篇关于我如何附加列添加到__MigrationHistory表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆