TPC继承错误

kia*_*osh 6 c# entity-framework ef-code-first ef-fluent-api

使用C#Entity Framework Codefirst和Fluent Api,我有一个TPC继承的奇怪问题.我有3类命名Person,Invoice并且PeriodicInvoice你可以看到下面.以下是我的代码摘要:

Invoice class及其配置类:

public class Invoice : InvoiceBase
{
    public Person User { get; set; }
}

public class InvoiceConfig : EntityTypeConfiguration<Invoice>
{
    public InvoiceConfig()
    {
        this.Map(m => { m.MapInheritedProperties(); m.ToTable("Invoices"); });
    }
}
Run Code Online (Sandbox Code Playgroud)

PeriodicInvoice 类及其配置:

public class PeriodicInvoice : InvoiceBase
{
    // Some extra properties.
} 

public class PeriodicInvoiceConfig : EntityTypeConfiguration<PeriodicInvoice>
{
    public PeriodicInvoiceConfig()
    {
       this.Property(x => x.SuspendOnExpire).IsRequired();
       this.Map(m => { m.MapInheritedProperties(); m.toTable("PeriodicInvoices"); });
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,会出现此错误:

实体类型"发票"和"人员"之间的关联"Invoice_User"无效.在TPC层次结构中,仅允许在大多数派生类型上使用独立关联.

我知道这意味着我应该将属性包含在User类中PeriodicInvoice,不要在课堂上使用它Invoice.

但是,有没有其他方法可以解决这个问题?谢谢.

Mah*_*bar 7

在TPC继承中,您不能在父类中指向另一个表的字段,因为您尝试将两个表指向另一个表,并且一个表尝试仅使用一个外键指向这两个表中的一个(这是不可能!).

我建议你使用TPT.这个链接可以帮到你.