gpt4 book ai didi

c# - 如何对 EF 中的多个条目进行显式加载?

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:33 24 4
gpt4 key购买 nike

我在网上看到了一些这样的显式加载示例:

引用: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();
}

或引用: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();
};

//生成的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 */)

这很好,但是如果我删除 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();
}
};

显然很大程度上取决于您面对的记录数量,恕我直言,如果您的外键和索引已优化,Include(然后是 JOIN 数据库端)是最佳选择,因为所有数据都是使用单个数据库调用检索,但实际上在某些情况下,更多不同的单个 SELECT 可能是一个有效选项。

关于c# - 如何对 EF 中的多个条目进行显式加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com