Search code examples
c#entities

How to eager load sub object of a sub object in Entities Framework


I have a Member class that has an array of MemberRoles attached to it. Each of the MemberRoles have a Role object attached to them. When I go to fetch my Member I know that I am going to need all the Role objects associated to the Member. I realize that I can eager load the MemberRoles with

var member = context.Members.Include("MemberRoles").SingleOrDefault(....)

I am speculating here (because my DBA wont give me permission to run profiler, grumble *grumble*) but I assume that the Role associated to the MemberRole object will lazy load once I start accessing it.

How can I eager load the Role object along with the MemberRoles when I form my original query?


Solution

  • Try

    var member = context.Members.Include("MemberRoles.Roles").SingleOrDefault(....)
    

    That should eager-load MemberRoles and the associated Roles.