繁体   English   中英

EF中的多态多对多关系

[英]Plymorphic Many to Many relationships in EF

我在一个实体类型上有一个多态集合,其中包含作为其子类型的对象的集合。 子类型都存储在数据库的不同表中。

我需要在流畅的API中映射此关系,以便我可以管理级联。 这是我拥有的实体结构的示例...

[Table("Reports", "Reporting")]
class Report
{
    [Key]
    public int Id { get; set; } 
    public virtual ICollection<Entry> Entries { get; set; }
}

abstract class Entry
{
   [Key]
   public int Id {  get; set; }
   public virtual ICollection<Report> Reports { get; set; }
}

[Table("InvoiceEntries", "Reporting")]
class InvoiceEntry : Entry
{
    public ICollection<Invoice> Invoices { get; set; }
}

... <other types> like InvoiceEntry each mapped to their own tables ...

在我的模型配置中,我有...

builder.Ignore<Entry>();

更新

如评论中所述,这里有一个关于多态关系的好答案...

https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-方针

...我的场景是“多具体关系表”,它具有多对多关系。

尽管描述很好,但似乎无法涵盖这种情况

嗯...好吧,这很奇怪...所以事实证明,这实际上仅在您有足够的能力让您知道从子基类型派生的类型时才让它知道,而该子基类型对此不起作用。

例子...

[Table("Reports", "Reporting")]
class Report
{
    [Key]
    public int Id { get; set; } 
    public virtual ICollection<Entry> Entries { get; set; }
}

abstract class Entry
{
   [Key]
   public int Id {  get; set; }
   public virtual ICollection<Report> Reports { get; set; }
}

[Table("InvoiceEntries", "Reporting")]
class InvoiceEntry : Entry
{
    public ICollection<Invoice> Invoices { get; set; }
}

class PaymentEntry
{
   [ForeignKey("Payment")]
   public int PaymentId { get;set; }

   public virtual Payment Payment { get; set; }
}

事实证明,存在一个问题,即报告对象需要使用流畅的API映射类型为PaymentEntry的子类,并关闭级联,以防止出现多个级联路径。

如果所有子类型仅具有InvoiceEntry样式,则多对多映射EF可以自行解决。

为了避免这种流畅的脚本编写,其他关系应避免级联问题或使所有子类型都成为多对多关系。

暂无
暂无

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

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