简体   繁体   English

如何使用双向查找多对多配置实体框架

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

相关问题 在nHibernate中如何正确进行双向多对多 - How correctly do a bi-directional many-to-many in nHibernate 如何在C#中正确地具有自动更新的一对多双向关系 - How to properly have an automatically updating one-to-many bi-directional relationship in C# 如何配置多对多实体框架关系,但要同一个对象? EF6 - How to configure many-to-many entity framework relationship, but to same object? EF6 如何使用面向 .net 框架的结构图在 asp.net core 中配置 hangfire 并解决双向依赖 - How to configure hangfire in asp.net core using structuremap targeting .net framework and resolve bi-directional dependency 如何在Entity Framework 6中删除多对多关系 - How delete many-to-many relation in Entity Framework 6 如何在实体框架中添加或删除多对多关系? - How to add or remove a many-to-many relationship in Entity Framework? 如何在实体框架中从数据库更新多对多关系? - How to update many-to-many relations from database in Entity Framework? 如何在 Entity Framework Core 中更新多对多关系中的表? - How to update tables in many-to-many relationship in Entity Framework Core? 如何在带有自定义多对多关联的Entity Framework中插入? - How to insert in Entity Framework with custom many-to-many association? 如何在Entity Framework中创建多对多映射? - How to create a many-to-many mapping in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM