我在 Stackoverflow 上的第一篇文章。所以如果我在这里不明白什么,请提前道歉:)
我有一个名为 ElementEntity 的抽象类。在这个类中,我有 2 个多对多关系,ChildRelations 和 ParentRelations。
我需要一个包含所有孩子和孙辈等的 id 的列表。形成一个特定的 ElementEntity。
我们不知道这棵树有多深,例如:Context.Hospitals().Include(b => b.ChildRelations).ThenInclude(c => c.child).ThenInclude etc.. 不是一个选项。( Hospital 是派生的 ElementEntity )
什么是最好的(最便宜的)方法?
作为指示,某个元素可能有数十万个后代对象。
public abstract class ElementEntity
{
public long Id {get; set;}
public virtual ICollection<BaseGroupParentChildRelation> ParentRelations { get; } = new List<BaseGroupParentChildRelation>();
public virtual ICollection<BaseGroupParentChildRelation> ChildRelations { get; } = new List<BaseGroupParentChildRelation>();
}
public class BaseGroupParentChildRelation
{
public long ParentId { get; set; }
public virtual ElementEntity Parent { get; set; }
public long ChildId { get; set; }
public virtual ElementEntity Child { get; set; }
}
可能的修复:
await projectDbContext.Set<BaseGroupParentChildRelation>().ToListAsync();
var Hotels = await projectDbContext.Hotels.Include(b =>
b.ChildRelations).ThenInclude(b => b.Child).ToListAsync();
酒店现在包括儿童、孙子、曾孙等等。等等。