【问题标题】:Entity Framework Code First - Invalid column name Discriminator实体框架代码优先 - 无效的列名鉴别器
【发布时间】:2013-04-29 08:05:10
【问题描述】:

codefirst 中的两个类

public partial class BaseEntity 
{
    public int ID { get; set; }
}

public partial class Fund : BaseEntity
{
   public int Name { get; set; }
}
public partial class InvestorFund : BaseEntity 
{
  public int FundID { get; set; }
}

映射类

this.Property(t => t.ID).HasColumnName("FundID");

My Code First Join SQL 查询

from fund in context.Funds

join investorFund in context.InvestorFunds on fund.ID equals investorFund.FundID

抛出Invalid column name Discriminator

【问题讨论】:

    标签: ef-code-first


    【解决方案1】:

    您需要告诉 Code First 这些类与表的关系。共有三个选项:

    • 每种类型的表 (TPT) 意味着在 Fund 和 InvestorFund 上定义的字段将进入它们自己的表中,而在 BaseEntity 上定义的属性将进入名为 BaseEntity 的表中。查询会更慢,因为每个实体现在必须组合来自多个表的字段。

      modelBuilder.Entity<Fund>().ToTable("Funds");
      modelBuilder.Entity<InvestorFund>().ToTable("InvestorFunds");
      
    • Table per heirarchy (TPH) 意味着 Fund、InvestorFund 和 BaseEntity 属性将全部合并到一个名为 BaseEntity 的表中,并且需要一个额外的字段来指示哪一行是哪种类型。这个额外的字段称为鉴别器

      modelBuilder.Entity<BaseEntity>()
        .Map<Fund>(m => m.Requires("Discriminator").HasValue("F"))
        .Map<InvestorFund>(m => m.Requires("Discriminator").HasValue("I"));
      
    • 每个具体类型的表 (TPC) 意味着 Fund 和 InvestorFund 都有自己的表,其中还包括其基类所需的任何字段。

      modelBuilder.Entity<Fund>().Map(m => {
                    m.MapInheritedProperties();
                    m.ToTable("Funds"); 
      }); 
      modelBuilder.Entity<InvestorFund>().Map(m => {
                    m.MapInheritedProperties();
                    m.ToTable("InvestorFunds"); 
      });
      

    【讨论】:

    • 你应该可以使用我在TPT下写的代码行来避免这个错误。
    • 在本例中,“类型”应为“鉴别器”。
    • 这对“NoteMap”类中的映射有什么影响
    • 什么是“NoteMap”?
    【解决方案2】:

    您需要运行迁移。

    转到包管理器控制台,然后在启用自动迁移后键入update-database

    【讨论】:

    • 这与迁移无关。
    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2013-10-25
    相关资源
    最近更新 更多