Eager Loading:

Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved using the Include() method.

In the following example, it gets all the students from the database along with it's standards using Include() method.

LINQ Query Syntax:

using (var context = new SchoolDBEntities())
{
var res = (from s in context.Students.Include("Standard")
where s.StudentName == "Student1"
select s).FirstOrDefault<Student>();
}

LINQ Method Syntax:

using (var ctx = new SchoolDBEntities())
{
stud = ctx.Students.Include("Standard")
.Where(s => s.StudentName == "Student1").FirstOrDefault<Student>(); }

The code shown above will result in following SQL query:

SELECT TOP (1)
[Extent1].[StudentID] AS [StudentID],
[Extent1].[StudentName] AS [StudentName],
[Extent2].[StandardId] AS [StandardId],
[Extent2].[StandardName] AS [StandardName],
[Extent2].[Description] AS [Description]
FROM [dbo].[Student] AS [Extent1]
LEFT OUTER JOIN [dbo].[Standard] AS [Extent2] ON [Extent1].[StandardId] = [Extent2].[StandardId]
WHERE 'Student1' = [Extent1].[StudentName]

Use Lambda Expression:

You can also use linq lambda expression in Include method. For this, take a reference ofSystem.Data.Entity namespace and use lambda expression as shown below.

using System;
using System.Data.Entity; class Program
{
static void Main(string[] args)
{ using (var ctx = new SchoolDBEntities())
{
stud = ctx.Students.Include(s => s.Standard)
.Where(s => s.StudentName == "Student1")
.FirstOrDefault<Student>(); }
}
}

The code shown above will result in the following SQL query:

SELECT TOP (1)
[Extent1].[StudentID] AS [StudentID],
[Extent1].[StudentName] AS [StudentName],
[Extent2].[StandardId] AS [StandardId],
[Extent2].[StandardName] AS [StandardName],
[Extent2].[Description] AS [Description]
FROM [dbo].[Student] AS [Extent1]
LEFT OUTER JOIN [dbo].[Standard] AS [Extent2] ON [Extent1].[StandardId] = [Extent2].[StandardId]
WHERE 'Student1' = [Extent1].[StudentName]

Load multiple levels of related entities:

You can also eagerly load multiple levels of related entities. The code snippet shown below loads related Student, Standard and Teachers:

using (var ctx = new SchoolDBEntities())
{
stud = ctx.Students.Include("Standard.Teachers")
.Where(s => s.StudentName == "Student1")
.FirstOrDefault<Student>();
}

Or using lambda expression as below.

using (var ctx = new SchoolDBEntities())
{
stud = ctx.Students.Include(s => s.Standard.Teachers)
.Where(s => s.StudentName == "Student1")
.FirstOrDefault<Student>();
}

The code shown above results in the following SQL query:

SELECT [Project2].[StudentID] AS [StudentID],
[Project2].[StudentName] AS [StudentName],
[Project2].[StandardId] AS [StandardId],
[Project2].[StandardName] AS [StandardName],
[Project2].[Description] AS [Description],
[Project2].[C1] AS [C1],
[Project2].[TeacherId] AS [TeacherId],
[Project2].[TeacherName] AS [TeacherName],
[Project2].[StandardId1] AS [StandardId1]
FROM ( SELECT
[Limit1].[StudentID] AS [StudentID],
[Limit1].[StudentName] AS [StudentName],
[Limit1].[StandardId1] AS [StandardId],
[Limit1].[StandardName] AS [StandardName],
[Limit1].[Description] AS [Description],
[Project1].[TeacherId] AS [TeacherId],
[Project1].[TeacherName] AS [TeacherName],
[Project1].[StandardId] AS [StandardId1],
CASE WHEN ([Project1].[TeacherId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM (SELECT TOP (1) [Extent1].[StudentID] AS [StudentID], [Extent1].[StudentName] AS [StudentName], [Extent1].[StandardId] AS [StandardId2], [Extent2].[StandardId] AS [StandardId1], [Extent2].[StandardName] AS [StandardName], [Extent2].[Description] AS [Description]
FROM [dbo].[Student] AS [Extent1]
LEFT OUTER JOIN [dbo].[Standard] AS [Extent2] ON [Extent1].[StandardId] = [Extent2].[StandardId]
WHERE 'updated student' = [Extent1].[StudentName] ) AS [Limit1]
LEFT OUTER JOIN (SELECT
[Extent3].[TeacherId] AS [TeacherId],
[Extent3].[TeacherName] AS [TeacherName],
[Extent3].[StandardId] AS [StandardId]
FROM [dbo].[Teacher] AS [Extent3]
WHERE [Extent3].[StandardId] IS NOT NULL ) AS [Project1] ON [Limit1].[StandardId2] = [Project1].[StandardId]
) AS [Project2]
ORDER BY [Project2].[StudentID] ASC, [Project2].[StandardId] ASC, [Project2].[C1] ASC

Learn how Entity Framework supports lazy loading of entities, in the next section.

Entity Framework Tutorial Basics(36):Eager Loading的更多相关文章

  1. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

  2. Entity Framework Tutorial Basics(38):Explicit Loading

    Explicit Loading with DBContext Even with lazy loading disabled, it is still possible to lazily load ...

  3. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  4. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  5. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

  6. Entity Framework Tutorial Basics(42):Colored Entity

    Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...

  7. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  8. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

  9. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

随机推荐

  1. linux下配置cvs服务器以及cvs常用命令

    .查看系统是否安装有cvs #cat /etc/services | grep cvspserver 看看是否有: cvspserver /tcp #CVS client/server operati ...

  2. Busybox shell脚本修改密码

    /****************************************************************************** * Busybox shell脚本修改密 ...

  3. THUWC2017

    100+20+20=140 还是很菜... T1 在美妙的数学王国中畅游 一棵树每个点有一个函数(sin,exp,一次函数),支持加边,删边,单点修改,查询一条路径在 $x$ 处的点值和 sol: 题 ...

  4. LeetCode 333. Largest BST Subtree

    原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...

  5. CF 36E Two Paths——欧拉路

    题目:http://codeforces.com/contest/36/problem/E 找出两条欧拉路覆盖无向图. 套上欧拉路模板.用过的边要记录. 注意 一个连通块.4个奇度数点 的情况是在两个 ...

  6. BZOJ1999 树网的核[数据加强版]

    1999: [Noip2007]Core树网的核 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1214  Solved: 336[Submit][St ...

  7. Erlang pool management -- RabbitMQ worker_pool

    在RabbitMQ中,pool 是以worker_pool 的形式存在的, 其主要用途之一是对Mnesia transaction 的操作. 而在RabbitMQ 中, pool 中的worker 数 ...

  8. Quartz 定时任务(含Redis)

    一  定时任务基础: MQ:Message Queue消息队列服务器:MSMQ.ActiveMQ.Redis等项目任务:确定邮件的发送,重置密码的发送(发送可能会很慢,而且有可能还需要重试),用消息队 ...

  9. 蓝桥杯 算法训练 ALGO-149 5-2求指数

     算法训练 5-2求指数   时间限制:1.0s   内存限制:256.0MB 问题描述 已知n和m,打印n^1,n^2,...,n^m.要求用静态变量实现.n^m表示n的m次方.已知n和m,打印n^ ...

  10. 机器学习:PCA(人脸识别中的应用——特征脸)

    一.思维理解 X:原始数据集: Wk:原始数据集 X 的前 K 个主成分: Xk:n 维的原始数据降维到 k 维后的数据集: 将原始数据集降维,就是将数据集中的每一个样本降维:X(i) . WkT = ...