我关于Stackoverflow的第一篇文章。所以如果我对这里的内容不了解,请提前道歉:)
我有一个名为ElementEntity的抽象类。 在这堂课中,我有2个多对多的关系,ChildRelations和ParentRelations。
我需要一个列表,其中包含所有子代和孙代等的ID。形成某个ElementEntity。
我们不知道这棵树有多深: Context.Hospitals()。Include(b => b.ChildRelations).ThenInclude(c => c.child).ThenInclude等..不是一个选择。 (医院是派生的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();
酒店现在包括孩子,孙子,曾孙等。