Tom*_*Tom 7 c# entity-framework
我在网上看到一些显式加载示例,如下所示:
参考:http ://www.entityframeworktutorial.net/EntityFramework4.3/explicit-loading-with-dbcontext.aspx
using (var context = new SchoolDBEntities())
{
context.Configuration.LazyLoadingEnabled = false;
var student = (from s in context.Students
where s.StudentName == "Bill"
select s).FirstOrDefault<Student>();
context.Entry(student).Collection(s => s.Courses).Load();
}
Run Code Online (Sandbox Code Playgroud)
或参考:http ://codingcanvas.com/loading-nested-entities-in-entityframework/
using (var context = new EmployeeContext())
{
var employee = context.Employees.FirstOrDefault();
context.Entry(employee).Reference(x => x.ContactDetails).Load();
context.Entry(employee).Reference(x => x.EmpDepartment).Load();
context.Entry(employee.EmpDepartment).Collection(x => x.DepartmentProjects).Load();
};
Run Code Online (Sandbox Code Playgroud)
//生成的SQL--------------------------------------------
SELECT TOP (1) 1.[EmployeeNo] AS [EmployeeNo],
1.[FirstName] AS [FirstName],
1.[LastName] AS [LastName],
1.[Age] AS [Age],
1.[DepartmentId] AS [DepartmentId],
1.[FunctionId] AS [FunctionId],
1.[TypeOfEmployee] AS [TypeOfEmployee],
1.[Project_ProjectCode] AS [Project_ProjectCode]
FROM [dbo].[Employees] AS 1
SELECT [Extent1].[EmployeeNo] AS [EmployeeNo],
[Extent1].[Address] AS [Address],
[Extent1].[Phone] AS [Phone],
[Extent1].[Fax] AS [Fax],
[Extent1].[Mobile] AS [Mobile],
[Extent1].[LocationCord] AS [LocationCord]
FROM [dbo].[EmployeeContacts] AS [Extent1]
WHERE [Extent1].[EmployeeNo] = 1 /* @EntityKeyValue1 */
SELECT [Extent1].[DepartmentId] AS [DepartmentId],
[Extent1].[DepartmentCode] AS [DepartmentCode],
[Extent1].[DepartmentName] AS [DepartmentName]
FROM [dbo].[Departments] AS [Extent1]
WHERE [Extent1].[DepartmentId] = 11 /* @EntityKeyValue1 */
SELECT [Extent1].[ProjectCode] AS [ProjectCode],
[Extent1].[ProjectName] AS [ProjectName],
[Extent1].[ProjectDescription] AS [ProjectDescription],
[Extent1].[Department_DepartmentId] AS [Department_DepartmentId]
FROM [dbo].[Projects] AS [Extent1]
WHERE ([Extent1].[Department_DepartmentId] IS NOT NULL)
AND ([Extent1].[Department_DepartmentId] = 11 /* @EntityKeyValue1 */)
Run Code Online (Sandbox Code Playgroud)
这很棒,但如果我删除FirstOrDefault()并放置Where(x=> x.Age > 20)它,它会返回一个集合,而不仅仅是 TOP(1)。我不能再用context.Entry(employee)了吧?因为它只适合单个条目对象。
现在,我该如何做与示例相同的操作,但我们不使用单个条目来Where选择多个条目并加载它们的引用?
Entry方法使您可以控制附加到当前上下文的实体,因此在使用它之前必须附加该实体。
实现目标的唯一方法是循环访问所有检索到的实体并加载引用的数据。
using (var context = new EmployeeContext())
{
var employee = context.Employees.Where(x=> x.Age > 20);
foreach( var item in employee)
{
context.Entry(item).Reference(x => x.ContactDetails).Load();
context.Entry(item).Reference(x => x.EmpDepartment).Load();
context.Entry(item.EmpDepartment).Collection(x => x.DepartmentProjects).Load();
}
};
Run Code Online (Sandbox Code Playgroud)
显然,很大程度上取决于您面对的记录数量,恕我直言,如果您的外键和索引经过优化,则包含(然后是 JOIN 数据库端)是最佳选择,因为所有数据都是通过单个数据库调用检索的,但实际上在在某些情况下,更多不同的单个 SELECT 可能是有效的选项。