延迟加载:延迟加载相关的数据

 using (var ctx = new SchoolDBEntities())
{
//Loading students only
IList<Student> studList = ctx.Students.ToList<Student>(); Student std = studList[]; //Loads Student address for particular Student only (seperate SQL query)
StudentAddress add = std.StudentAddress;
}

首先查询Student表

SELECT
[Extent1].[StudentID] AS [StudentID],
[Extent1].[StudentName] AS [StudentName],
[Extent1].[StandardId] AS [StandardId]
FROM [dbo].[Student] AS [Extent1]

再查询StudentAddress表

exec sp_executesql N'SELECT
[Extent1].[StudentID] AS [StudentID],
[Extent1].[Address1] AS [Address1],
[Extent1].[Address2] AS [Address2],
[Extent1].[City] AS [City],
[Extent1].[State] AS [State]
FROM [dbo].[StudentAddress] AS [Extent1]
WHERE [Extent1].[StudentID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1

可以为指定的属性或整个上下文关闭延迟加载的功能,关闭属性的延迟加载功能就是不要设置属性为virtual

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq; public partial class SchoolDBEntities : DbContext
{
public SchoolDBEntities(): base("name=SchoolDBEntities")
{
this.Configuration.LazyLoadingEnabled = false;
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
}

延迟加载的规则:

1.context.Configuration.ProxyCreationEnabled必须为true

2.context.Configuration.LazyLoadingEnabled必须为true

3.导航属性必须定义为public virtual

最新文章

  1. Android Studio快捷键
  2. C#语言基础——结构体和枚举类型
  3. Android播放视频
  4. c语言编程之sglib库的简单使用
  5. NSString 筛选和最后一个空白、空行,多换行成一个新行
  6. [Linux]当一个棘手问题需要即可定位,如何协助开发,缩小定位范围
  7. iOS ReactiveCocoa(RAC)学习详解
  8. 最近公共祖先LCA(Tarjan算法)的思考和算法实现
  9. python3学习笔记(1)
  10. mysql启动日志文件log_bin
  11. Entry
  12. Dynamics CRM2016 WebApi查询之alternate key
  13. ERP-非财务人员的财务培训教(二)------如何评价公司/部门经营业绩
  14. NodeJs之邮件(email)发送
  15. JavaScript基础笔记(一)基本概念
  16. numpy delete
  17. [javaEE] Servlet的手动配置
  18. ASP.NET WebApi使用Swagger生成api说明文档
  19. Spring Boot项目Maven Build报错的解决方法
  20. ballerina 学习二十六 项目docker 部署&amp;&amp; 运行(二)

热门文章

  1. xgboost 特征选择,筛选特征的正要性
  2. Django--基础补充
  3. explicit 和 implicit 的用法
  4. Apache中KeepAlive 配置
  5. Hp服务器 iLO3 使用方法
  6. Windows下搭建React Native Android开发环境
  7. web安全之SQL注入---第三章 如何寻找sql注入?
  8. Linux中的关机
  9. github常见错误
  10. php自定义函数: 遍历文件夹及其子文件夹