简体   繁体   English

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

[英]How to do explicit loading for multiple entries in EF?

I see a few explicit loading examples on the web like this:我在网上看到一些显式加载示例,如下所示:

ref: http://www.entityframeworktutorial.net/EntityFramework4.3/explicit-loading-with-dbcontext.aspx参考: 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();
    }

Or ref: http://codingcanvas.com/loading-nested-entities-in-entityframework/或参考: 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 Generated -------------------------------------------- //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 */)

That is great, but if I remove FirstOrDefault() and put Where(x=> x.Age > 20) it returns me a collection not just TOP(1).太好了,但是如果我删除FirstOrDefault()并放置Where(x=> x.Age > 20)它会返回一个集合,而不仅仅是 TOP(1)。 I can't use context.Entry(employee) any more right?我不能再使用context.Entry(employee)了吧? since it is only suitable for a single entry object.因为它只适用于单个入口对象。

Now, how do I do the same as the examples, but instead of single entry we use Where to select multiple entries and load their references?现在,我该如何做与示例相同的操作,但我们使用Where来选择多个条目并加载它们的引用而不是单个条目?

Entry method give you the control over an entity attached to the current context, so before use it the entity must be attached. Entry方法使您可以控制附加到当前上下文的实体,因此在使用它之前必须附加实体。

The only way to achieve your target is to cycle on all your retrieved entities and Load referenced data.实现目标的唯一方法是循环访问所有检索到的实体并加载引用的数据。

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

Obviously much depends on how many records you are facing, IMHO, if your Foreign Keys and Indexes are optimized, the Include (then a JOIN database side) is the best choice, because all data are retrieved with a single database call, but indeed in some cases more different single SELECT could be a valid option.显然,这很大程度上取决于您面对的记录数量,恕我直言,如果您的外键和索引已优化,则Include (然后是 JOIN 数据库端)是最佳选择,因为所有数据都是通过单个数据库调用检索的,但确实在在某些情况下,更多不同的单个 SELECT 可能是一个有效的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM