贪婪加载是指查询一个类型实体的时候同时查询与实体关联的类型

通过Include()方法实现

using (var context = new SchoolDBEntities())
{
var stud1 = (from s in context.Students.Include("Standard")
where s.StudentName == "Student1"
select s).FirstOrDefault<Student>();
}
using (var ctx = new SchoolDBEntities())
{
var stud1 = ctx.Students.Include("Standard")
.Where(s => s.StudentName == "Student1").FirstOrDefault<Student>(); }
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]
using System;
using System.Data.Entity; class Program
{
static void Main(string[] args)
{ using (var ctx = new SchoolDBEntities())
{
var stud1 = ctx.Students.Include(s => s.Standard)
.Where(s => s.StudentName == "Student1")
.FirstOrDefault<Student>(); }
}
}
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:

using (var ctx = new SchoolDBEntities())
{
var stud1 = ctx.Students.Include("Standard.Teachers")
.Where(s => s.StudentName == "Student1")
.FirstOrDefault<Student>();
}
using (var ctx = new SchoolDBEntities())
{
var stud1 = ctx.Students.Include(s => s.Standard.Teachers)
.Where(s => s.StudentName == "Student1")
.FirstOrDefault<Student>();
}
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

EntityFramework 学习 一 Eager Loading的更多相关文章

  1. EntityFramework 学习 一 Explicit Loading with DBContext

    即使延迟加载不能使用,也可以通过明确的调用来延迟加载相关实体 使用DBEntryEntity来完成 using (var context = new SchoolDBEntities()) { //D ...

  2. EntityFramework 学习 一 Lazy Loading 1

    延迟加载:延迟加载相关的数据 using (var ctx = new SchoolDBEntities()) { //Loading students only IList<Student&g ...

  3. EntityFramework 学习 一 Lazy Loading

    延迟加载:延迟加载相关的数据 using (var ctx = new SchoolDBEntities()) { //Loading students only IList<Student&g ...

  4. Lazy Loading | Explicit Loading | Eager Loading in EntityFramework and EntityFramework.Core

    EntityFramework Eagerly Loading Eager loading is the process whereby a query for one type of entity ...

  5. Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading

    Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...

  6. EF Core 2.1 中的 Eager loading、Explicit loading和LazyLoading (转自MSDN)

    Entity Framework Core allows you to use the navigation properties in your model to load related enti ...

  7. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  8. entityframework学习笔记--001

    最近想重新好好学习一下entityframework,于是在院子里找到了一篇不错的博客.下面把学习的过程记录下来,方便以后复习. 学习过程参考大神的博客:http://www.cnblogs.com/ ...

  9. EntityFramework 学习资料

    1.EF框架step by step 2.Entity Framework Code First 学习日记 3.[译著]Code First :使用Entity. Framework编程 4.Enti ...

随机推荐

  1. 在caffe中用训练好的 caffemodel 来分类新的图片所遇到的问题

    结合之前的博客: http://www.cnblogs.com/Allen-rg/p/5834551.html#3949333 用caffemodel去测试单通道的图像(mnist数据集)时,出现了问 ...

  2. baksmali反编译出现:UNEXPECTED TOP-LEVEL ERROR:....Too many open files

    解包大型apk文件,可能会出现例如以下错误, UNEXPECTED TOP-LEVEL ERROR: java.util.concurrent.ExecutionException: java.io. ...

  3. python 中的"switch"用法

    转载:http://python.jobbole.com/82008/ 为什么Python中没有Switch/Case语句? 不同于我用过的其它编程语言,Python 没有 switch / case ...

  4. 关于.cap文件分析

    CAP文件是比较通用的一种文件格式,基本上大多数抓包软件都支持以此格式将捕获的网络数据包存储下来.    需要说明的是,CAP文件存储下来的,一般都是网络数据帧.不同网络传输协议下的帧格式是有差异的, ...

  5. linux网络及防火墙配置命令

      /etc/sysconfig/network 包括主机基本网络信息,用于系统启动 /etc/sysconfig/network-script/ 此目录下是系统启动最初始化网络的信息 /etc/sy ...

  6. centos6下手工编译vitess

    vitess是youtub开源的一款mysql代理,在ubuntu下编译非常方便.可是在centos下且不能訪问google的情况下坑比較多.近期依据其bootstrap.sh脚本手工编译成功.把过程 ...

  7. hadoop生态系统学习之路(八)hbase与hive的数据同步以及hive与impala的数据同步

    在之前的博文中提到,hive的表数据是能够同步到impala中去的. 一般impala是提供实时查询操作的,像比較耗时的入库操作我们能够使用hive.然后再将数据同步到impala中.另外,我们也能够 ...

  8. Linux高并发应用类型对系统内核的优化

    Linux操作系统内核参数优化 net.ipv4.tcp_max_tw_buckets = net.ipv4.ip_local_port_range = net.ipv4.tcp_tw_recycle ...

  9. js校验密码强度

    网上转载的一段代码,留着以后用, js文件: //判断输入密码的类型 function CharMode(iN){ if (iN>=48 && iN <=57) //数字 ...

  10. c_str()方法使用

    语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过 ...