There are three EF entities:

public User { public int ID {get;set;} public string Name {get;set;} public virtual ICollection<Permission> Permissions; } public Permission { public int ID {get;set;} public string Name {get;set;} public virtual ICollection<User> Users; public virtual ICollection<Domain> Domains; } public Domain { public int ID {get;set;}; public string Name {get;set;} public int PermissionID {get;set;} public Permission Permission {get;set;} } 

The project uses Code First. As you can see, entities are related by relationships:

User ** (M) - (M) ** Permission

Permission ** (1) - (M) ** Domain

There is a generic repository. I get from the context a collection of users as follows:

 List<User> AllUsers = context.GetAllIncluding<User>(u=>u.Permissions).ToList(); 

I get a collection of users, each element of which includes the corresponding collection of instances of the Permission class, and now in these instances the link to the collection of instances of the Domain class contains null.

Question: How to get a collection of users, each element of which will contain the Permissions collection, each element of which, in turn, will contain the Domains collection, etc.

    2 answers 2

    in order to use lazy loading you need to make virtual properties.


    In your particular case:

     public User { public int ID {get;set;} public string Name {get;set;} public virtual ICollection<Permission> Permissions {get;set;} } public Permission { public int ID {get;set;} public string Name {get;set;} public virtual ICollection<User> Users {get;set;} public virtual ICollection<Domain> Domains {get;set;} } public Domain { public int ID {get;set;}; public string Name {get;set;} public int PermissionID {get;set;} public virtual Permission Permission {get;set;} } 

    Properties marked with the virtual modifier, the so-called navigation properties when referring to which the Entity Framework will pull up the necessary records automatically

      Try this if I understand the method signatures correctly.

       context.GetAllIncluding<User>(u => u.Permissions.SelectMany(p => p.Domains )).ToList(); 

      But be careful - there may be a terrible request ...

      • And what if everywhere to use virtual-properties? Will it slow down the base much? The base is small - about 20 entities. - klutch1991
      • Yes, slow down. Lazy loading slows everything down. - Pavel Mayorov
      • Those. is it optimal to selectively leave the virtual modifier? And, say, on entities that have a lot of navigation properties left without virtual and use GetAllIncluding <TEntity>? - klutch1991
      • one
        If the request is simple, then yes. And if there are complex conditions in the request, you need to use the download in several stages (first users, then permissions for all of them - etc.) - Pavel Mayorov
      • one
        @ klutch1991 and yes - the virtual should be left not for those entities that have few navigation properties - but for those where they rarely follow these properties. - Pavel Mayorov