I have the following entity:
public class User
{
public int UserId { get; set;}
public string UserName { get; set;};
public int UserSupervisorId { get; set; }
[ForeignKey("UserSupervisorId"}
public User UserSupervisor { get; set;};
}
When I load a given User
I always want to load their UserSupervisor
and continue recursively to the top loading each UserSupervisor
at each level.
When use the Include
method it will only load the immediate parent and no further. The code I use for this is:
var user = dbContext.Users.Include(u.UserSupervisor).SingleAsync(u => u.UserId == userId);
I can add a ThenInclude
which will go up one further level.
I thought that relationship fix up would cause the graph to be loaded, granted I'm going from child to parent.
Is there a mechanism to do this in EF out of the box?