TPC继承错误

编程入门 行业动态 更新时间:2024-10-22 11:35:51
本文介绍了TPC继承错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有使用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继承错误

本文发布于:2023-11-17 04:44:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   TPC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!