【问题标题】:How do I add an additional column to the __MigrationHistory table?如何向 __MigrationHistory 表添加额外的列?
【发布时间】:2014-06-03 21:17:28
【问题描述】:

根据Customizing the Migrations History Table,我应该能够添加一列,但是我找不到任何关于如何实际添加新列的示例。

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

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 实体添加新的派生类型?

【问题讨论】:

  • 您是否尝试过您发布的链接中的示例我只看到在您的代码中调用的两种方法之一..
  • 该链接没有讨论添加列。
  • @DavidG:“为什么要自定义迁移历史表?”下列表中的最后一个项目符号...“您需要为给定版本的上下文存储其他数据,因此您需要添加一个表格的附加列”
  • @Chris 是的,但没有说明怎么做。
  • @DavidG:正确,这就是我问这个问题的原因。文章说您应该能够做到,但我没有看到任何其他资源可以解释如何做到这一点。

标签: c# entity-framework-6 entity-framework-migrations


【解决方案1】:

首先,您需要为历史记录行实体创建一个新类。例如:

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

接下来我们需要一个上下文,就像我们对普通 EF 操作所做的一样,不过这次我们继承自 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");
    }
} 

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

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

最后,通过修改我们的 web.config 来应用配置:(您必须填写自己的命名空间和应用程序集:

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

这样做的副作用是,如果/当您启用迁移时,您需要明确说明您正在使用的上下文。这显然是因为您的程序集中现在有两种上下文类型(除非您将它们分开。)所以您现在需要运行如下命令:

enable-migrations -ContextTypeName Namespace.YourContextClass

【讨论】:

  • 谢谢哥们,写得真好。我错过了有关为 HistoryContext “更新” DbSet 的内容。
  • 很好的答案,但是似乎没有任何方法可以将数据注入其中。可能对稍后在另一个进程中添加数据有用。
  • 它看起来很有希望,它确实创建了带有指定附加列的表。只是它还创建了它原来的 dbo.__MigrationHistory,这就是它实际放置历史行的地方
猜你喜欢
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
相关资源
最近更新 更多