问题描述
根据 自定义迁移历史记录表,我应该能够添加一个列,但是我找不到任何有关如何实际添加新列的示例.
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
实体.
...but I can't modify the HistoryRow
entity.
我是否应该基于 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 操作所做的一样,不过这次我们继承自 HistoryContext
:
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));
}
}
最后,通过修改我们的 web.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 表添加额外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!