Blaise
Blaise

Reputation: 22212

How to do "nested" eager loading in Entity Framework?

Scenario:

Employee:     EmployeeId   (PK)    DepartmentId_FK  (FK)   Name and other fields
Department:   DepartmentId (PK)    CompanyId_FK     (FK)   Name and other fields
Company:      CompanyId    (PK)                            Name and other fields

How can I build a linq query that can join the three tables and returns EmployeeName, DepartmentName and CompanyName?

the following syntax does not work because there is no Navigation Property names Company on Employee.cs.

var model = DataContext.Employee
                .Include("Employer")
                .Include("Company");

What is the correct way to do this?

Upvotes: 2

Views: 3045

Answers (1)

Jomit
Jomit

Reputation: 616

Try this :

 var data = DataContext.Employees.Include("Department").Include("Department.Company");

But use this with caution as eager loading can hurt back if there is large amount of data.

Upvotes: 3

Related Questions