TPC继承错误 [英] TPC Inheritance Error
问题描述
我有使用C#实体框架Codefirst和流畅API TPC继承一个奇怪的问题。
我有3类名为人
,发票
和 PeriodicInvoice
,你可以看到下面。
这里是我的代码摘要:
I've got an strange problem with TPC inheritance using C# Entity Framework Codefirst and Fluent Api.
I have 3 Classes named Person
, Invoice
and PeriodicInvoice
as you can see below.
Here is a summary of my code:
发票
类和它的配置类:
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
类和它的配置:
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:
实体类型的发票和人之间的关联Invoice_User是无效的。在TPC层次的独立协会只允许在最派生类型。
The association 'Invoice_User' between entity types 'Invoice' and 'Person' is invalid. In a TPC hierarchy independent associations are only allowed on the most derived types.
我知道这意味着我应该包括财产用户
类 PeriodicInvoice
不要在课堂上使用发票
I know it means that I should include the property User
to class PeriodicInvoice
and don't use it in class Invoice
.
不过,是不是有其他的方式来解决这个问题?
感谢。
But, Isn't there any other way to solve this problem? Thanks.
推荐答案
在TPC继承,你不能在父类中的字段指向另一个表,因为你想点两个表到另一个表,并试图将指向只使用一个外键这两个表中的一个一个表(这是不可能的!)。
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!).
我建议你使用TPT。的这个链接可以帮你。
I suggest you to use TPT. This link can help you.
这篇关于TPC继承错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!