Entity Framework Tutorial Basics(36):Eager Loading
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的更多相关文章
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- Entity Framework Tutorial Basics(38):Explicit Loading
Explicit Loading with DBContext Even with lazy loading disabled, it is still possible to lazily load ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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 ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- 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 ...
- 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 ...
随机推荐
- git教程4-创建分支与删除分支
一.分支的理解 分支,即branch,用于从主分支(master)中新开辟出一个分支,用于对文件进行修改.这部分修改的内容在新的分支未融合到主分支的情况下,主分支是看不见的.新的分支相当于开辟了新的修 ...
- 排序算法 java实现2
继续排序算法 4.冒泡排序 从第一个开始,跟后面一个数比较,如果比后面大就交换位置,这样没完成一轮,就可以把最大的选出来 public static <T extends Comparable& ...
- 统计数字noip2007
7909:统计数字 总时间限制: 1000ms 内存限制: 65536kB 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过1000 ...
- 如何测试远端TCP和UDP端口是否开放
项目遇到问题时首先排查网络是否正常是一个重要的方面.遇到很多次,同事找我解决问题,最后发现却是IP或端口不通的问题.然而就是这么个简单的问题,对方却花费了甚至一天的时间排查原因. 现在大部分项目都是用 ...
- TexStudio 非常好用的Latex软件
先大概写一下,免得忘了,等有时间详细补充. 跨平台.免费. 语法高亮 方便的公式.符号选择界面 可以配置Latex,pdflatex,xelatex等默认编译命令 集成了pdf阅读器,可在阅读器中浏览 ...
- shell脚本中常用命令
1 Shell中的特殊符号 1.1 $ 美元符号.用来表示变量的值.如变量NAME的值为Mike,则使用$NAME就可以得到“Mike”这个值. 1.2 ...
- Object-C类、方法、构造函数(2)
Object-C 代码分为三部分:.h文件..m文件及调用文件 .h源文件 #import <Foundation/Foundation.h> @interface Student:NSO ...
- C语言库在不同系统下的后缀
C语言的静态库与动态库对比分析,各有长短 库: 指由标准常用函数编译而成的文件,旨在提高常用函数的可重用性,减轻开发人员负担.常用的sdtio.h,math.h等 库 ...
- ThreadPoolExecutor之一:使用基本介绍
一.concurrent包中的线程池的简单介绍 线程池按照线程数量可以分为:一是固定线程数量的线程池:二是可变数量的线程池. 线程池按照执行时间可以分为:一是立即执行线程池:二是延时线程池. Thre ...
- c#在sql中存取图片image示例
这篇文章主要介绍了c#在sql中存取图片image示例,需要的朋友可以参考下 (1)控制台应用程序下演示插入图片 复制代码 代码如下: public void InsertIMG() { //将需要存 ...