简体   繁体   English

TPC继承错误

[英]TPC Inheritance Error

I've got an strange problem with TPC inheritance using C# Entity Framework Codefirst and Fluent Api. 使用C#Entity Framework Codefirst和Fluent Api,我有一个TPC继承的奇怪问题。 I have 3 Classes named Person , Invoice and PeriodicInvoice as you can see below. 我有3个名为PersonInvoicePeriodicInvoice类,如下所示。 Here is a summary of my code: 以下是我的代码摘要:

Invoice class and its configuration class: Invoice类及其配置类:

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

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

PeriodicInvoice class and its configuration: 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"); });
    }
}

When I run the code, this error appears: 当我运行代码时,会出现此错误:

The association 'Invoice_User' between entity types 'Invoice' and 'Person' is invalid. 实体类型“发票”和“人员”之间的关联“Invoice_User”无效。 In a TPC hierarchy independent associations are only allowed on the most derived types. 在TPC层次结构中,仅允许在大多数派生类型上使用独立关联。

I know it means that I should include the property User to class PeriodicInvoice and don't use it in class Invoice . 我知道这意味着我应该包括财产UserPeriodicInvoice并在课堂上不使用它Invoice

But, Isn't there any other way to solve this problem? 但是,有没有其他方法可以解决这个问题? Thanks. 谢谢。

In TPC inheritance you can't have a field in parent class that points to another table because you are trying to point two tables to another table and one table that tries to point to one of these two tables using only one foreign key (and that's impossible!). 在TPC继承中,您不能在父类中指向另一个表的字段,因为您尝试将两个表指向另一个表,并且一个表尝试仅使用一个外键指向这两个表中的一个(这是不可能!)。

I suggest you to use TPT. 我建议你使用TPT。 This link can help you. 这个链接可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM