TPC 继承错误
TPC Inheritance Error
我在使用 C# Entity Framework Codefirst 和 Fluent Api 的 TPC 继承方面遇到了一个奇怪的问题。
我有 3 个 类,名为 Person
、Invoice
和 PeriodicInvoice
,如下所示。
这是我的代码摘要:
Invoice
class 及其配置 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"); });
}
}
PeriodicInvoice
class及其配置:
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"); });
}
}
当我运行代码时,出现这个错误:
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.
我知道这意味着我应该包括 属性 User
到 class PeriodicInvoice
并且不要在 class [=13] 中使用它=].
但是,有没有其他方法可以解决这个问题呢?
谢谢。
在 TPC 继承中,您不能在父 class 中有指向另一个 table 的字段,因为您试图将两个 table 指向另一个 table 和一个 table 试图仅使用一个外键指向这两个 table 之一(这是不可能的!)。
我建议你使用TPT。 This link可以帮到你。
我在使用 C# Entity Framework Codefirst 和 Fluent Api 的 TPC 继承方面遇到了一个奇怪的问题。
我有 3 个 类,名为 Person
、Invoice
和 PeriodicInvoice
,如下所示。
这是我的代码摘要:
Invoice
class 及其配置 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"); });
}
}
PeriodicInvoice
class及其配置:
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"); });
}
}
当我运行代码时,出现这个错误:
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.
我知道这意味着我应该包括 属性 User
到 class PeriodicInvoice
并且不要在 class [=13] 中使用它=].
但是,有没有其他方法可以解决这个问题呢? 谢谢。
在 TPC 继承中,您不能在父 class 中有指向另一个 table 的字段,因为您试图将两个 table 指向另一个 table 和一个 table 试图仅使用一个外键指向这两个 table 之一(这是不可能的!)。
我建议你使用TPT。 This link可以帮到你。