My first post on Stackoverflow. So Apologies in advance if I do not understand something here :)
I have a abstract class called ElementEntity. Inside this class i have 2 many to many relations, ChildRelations and ParentRelations.
I need a list with id's from all the Children and grandchildren etc.. form a certain ElementEntity.
We don't know how deep this tree is so somthing like: Context.Hospitals().Include(b => b.ChildRelations).ThenInclude(c => c.child).ThenInclude etc.. is not an option. ( Hospital is a derived ElementEntity )
What would be the best (most inexpensive) approach?
As indication a certain element could have hundreds of thousands of offspring objects.
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; }
}
Possible fix:
await projectDbContext.Set<BaseGroupParentChildRelation>().ToListAsync();
var Hotels = await projectDbContext.Hotels.Include(b =>
b.ChildRelations).ThenInclude(b => b.Child).ToListAsync();
Hotels now include children, grandchildren, great-grandchildren etc.. etc..