[英]How to configure Entity Framework many-to-many with bi-directional lookup
I don't know how to properly title this question, so I did my best to come up with a descriptive title. 我不知道如何正确地为这个问题加上标题,所以我尽力拿出一个描述性的标题。
Basically, I have an Entity Framework entity that looks like the following ... 基本上,我有一个如下所示的Entity Framework实体...
public class LegalDocument
{
public int ExampleId { get; set; }
public virtual ICollection<LegalDocument> LegalDocuments { get; set; }
}
The LegalDocument
entity needs to be able to have references to other LegalDocument
s. LegalDocument
实体需要能够引用其他LegalDocument
。 In the reverse direction, I need to be able to see what other LegalDocument
s reference this LegalDocument
. 相反,我需要能够看到其他LegalDocument
引用了此LegalDocument
。
So it's like several Entity Framework navigational properties, but I don't know how to specify that one of the properties is for LegalDocument
s referenced within this LegalDocument
, versus the other LegalDocument
s that reference this one. 所以,这就像一些实体框架的导航性能,但我不知道如何指定的属性之一是LegalDocument
s此中引用LegalDocument
,与其他LegalDocument
引用这一秒。
Any ideas? 有任何想法吗?
You can maintain the relationship in code having this method in the LegalDocument
class. 您可以在LegalDocument
类中的具有此方法的代码中维护关系。
public void RelateDocument(LegalDocument document)
{
LegalDocuments.Add(document);
document.LegalDocuments.Add(this);
}
Use it to relate documents instead of using LegalDocuments.Add()
directly. 使用它来关联文档,而不是直接使用LegalDocuments.Add()
。
This way LegalDocuments
navigation property links to documents that current instance added and documents that added the current instance. 这样, LegalDocuments
导航属性链接到当前实例添加的文档和添加当前实例的文档。
最终可以使用流畅的API的HasMany().WithMany()
轻松实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.