如何在Entity Framework中进行“嵌套”的预先加载?

时间:2012-11-30 18:37:49

标签: linq entity-framework

情景:

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

如何构建可以连接三个表并返回EmployeeName,DepartmentName和CompanyName的linq查询?

以下语法不起作用,因为Company上没有导航属性名称Employee.cs

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

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

试试这个:

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

但请谨慎使用,因为如果有大量数据,急切的加载可能会受到影响。