I'm having some trouble trying to configure some entity with self reference. My entities looks pretty much like the following:
public class MainEntity
{
public Guid Id {get; private set;}
...
public IReadOnlyList<Foo> Foos => _foos;
private readonly List<Foo> _foos {get; private set;} = new List<Foo>();
}
public class Foo
{
public Guid Id {get; private set;}
public Guid MainEntityId {get; private set;}
public IReadOnlyList<Bar> Bars => _bars;
private readonly List<Bar> _bars {get; private set;} = new List<Bar>();
}
public class Bar
{
public Guid Id {get; private set;}
public Guid? FooId {get; private set;}
public Guid? ParentBarId {get; private set;}
public IReadOnlyList<Bar> Bars => _bars;
private readonly List<Bar> _bars {get; private set;} = new List<Bar>();
}
So basically the MainEntity has list of Foos which has list of Bars with possible inner Bars list (you can look at MainEntity as some user profile; Fools as list of user posts; Bars as comments of a post or replays of some parent comment)
Initially I thought everything was fine, but later noticed that by doing context.MainEntities.Include(x => x.Foos).ThenInclude(x => x.Bars).First(); I only get first layer of Bars (that has FooId) and somebar.Bars is always empty.
I could use ThenInclude(x => x.Bars) multiple times but that doesn't really make sense since max depth isn't limited and would hurt a retrieval performance.
I tried having Bar.FooId ar not nullable and populate it but had no luck (context.SaveChangesAsync() couldn't resolve the relationship, probably because of missconfiguration in modelbuilder). Also im not sure if it won't cause additional issues like Foo containing all of Bars with FooId even tho Bar has ParentBar...
Sometimes my explanations can be confusing, so let me know if any clarification is needed.
