简体   繁体   English

EF中的多态多对多关系

[英]Plymorphic Many to Many relationships in EF

I have a polymorphic collection on one entity type that contains a collection of objects that are the child types of it. 我在一个实体类型上有一个多态集合,其中包含作为其子类型的对象的集合。 the child types are all stored in different tables in the db. 子类型都存储在数据库的不同表中。

I need to map this relationship in the fluent API so I can manage the cascades. 我需要在流畅的API中映射此关系,以便我可以管理级联。 Here's a sample of the entity structure I have ... 这是我拥有的实体结构的示例...

[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 ...

In my model configuration I have ... 在我的模型配置中,我有...

builder.Ignore<Entry>();

Update 更新

As mentioned in the comments, there's a great answer here on polymorphic relationships ... 如评论中所述,这里有一个关于多态关系的好答案...

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

... My scenario is the "Table Per Concrete Type" one with a Many to Many Relationship as the polymorphic one. ...我的场景是“多具体关系表”,它具有多对多关系。

Whilst that description is great it doesn't seem to cover this scenario 尽管描述很好,但似乎无法涵盖这种情况

Huh ... ok that was strange ... so it turns out that this actually only works if you're nice enough to let it know when you have a derived type from the child base type that doesn't work this way. 嗯...好吧,这很奇怪...所以事实证明,这实际上仅在您有足够的能力让您知道从子基类型派生的类型时才让它知道,而该子基类型对此不起作用。

Example ... 例子...

[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; }
}

It turns out the issue there is that the report object needs to have that PaymentEntry sub typed mapped using the fluent API and the cascade turned off in order to prevent multiple cascade paths. 事实证明,存在一个问题,即报告对象需要使用流畅的API映射类型为PaymentEntry的子类,并关闭级联,以防止出现多个级联路径。

If all child types only have the InvoiceEntry style many to many mapping EF can figure it out by itself. 如果所有子类型仅具有InvoiceEntry样式,则多对多映射EF可以自行解决。

To avoid this either fluent script the other relationship to avoid the cascade problem or make all child types in to many to many relationships. 为了避免这种流畅的脚本编写,其他关系应避免级联问题或使所有子类型都成为多对多关系。

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

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