1

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?

  • Does this answer your question? [How does Entity Framework work with recursive hierarchies? Include() seems not to work with it](https://stackoverflow.com/questions/1308158/how-does-entity-framework-work-with-recursive-hierarchies-include-seems-not-t) – TimS Jun 29 '22 at 22:19

0 Answers0