Lazy Loading:

One of the important functions of Entity Framework is lazy loading. Lazy loading means delaying the loading of related data, until you specifically request for it. For example, Student class contains StudentAddress as a complex property. So, the context first loads all the students from the database, then it will load the address of a particular student when we access StudentAddress property as shown below.

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

The code shown above will result in two SQL queries. First, it will fetch all students:

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

The, it will send the following query when we get the reference of 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

However, you can also turn off lazy loading for a particular property or an entire context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false:

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

Rules for lazy loading:

  1. context.Configuration.ProxyCreationEnabled should be true.
  2. context.Configuration.LazyLoadingEnabled should be true.
  3. Navigation property should be defined as public, virtual. Context will NOT do lazy loading if the property is not defined as virtual.

Learn how to load entities explicitly in the next section.

Entity Framework Tutorial Basics(37):Lazy Loading的更多相关文章

  1. 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 ...

  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. metaclass 了解一下

    创建类的两种方式 方式一: class Foo(object,metaclass=type): CITY = "bj" def func(self,x): return x + 1 ...

  2. php实现word在线浏览功能。

    http://laoniangke.com/php/2012/10/08/php-doc-webview.html

  3. python使用 db.select 返回的数据只能遍历一次

    python中通过find从mongo中查出的数据,或者通过select返回的数据,其实返回的是游标,当你进行便利一次之后,游标指向最后, 所以当你再一次进行便利时,便出现数据为空的现象. 解决办法: ...

  4. BeetleX高性能通讯开源组件

    net core高性能通讯开源组件BeetleX https://www.cnblogs.com/smark/p/9617682.html BeetleX beetleX是基于dotnet core实 ...

  5. ASP.net之HttpModel

    HttpModule是向实现类提供模块初始化和处置事件.当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于H ...

  6. SQL Sever 学习系列之一

    SQL Sever 学习系列之一 本学习系列,从实际工作需要中积累,对于一个新手而言,写出几条漂亮的查询语句,应该是可以受启发的. 一.问题的需求是:员工薪酬发放,现有资金能发放多少人,哪些人应得? ...

  7. 上传文件csv 导入功能

    HTML代码: <script> function uploadCsv() { $('#chooseCsvFile').click(); } function doUploadCsv() ...

  8. array_diff函数的注意事项

    array_diff — 计算数组的差集 说明: array array_diff ( array $array1 , array $array2 [, array $... ] ) 对比返回在 ar ...

  9. 机器学习:SVM(scikit-learn 中的 SVM:LinearSVC)

    一.基础理解 Hard Margin SVM 和 Soft Margin SVM 都是解决线性分类问题,无论是线性可分的问题,还是线性不可分的问题: 和 kNN 算法一样,使用 SVM 算法前,要对数 ...

  10. SA读书笔记1

    SA的基本任务: 帐户: 为新用户增设帐号,将不再活动的帐号删除,帐号存活期事务(忘记密码等).把用户的主目录放在什么位置.在哪些机器上创建帐号. 硬件:识别并使用新硬件.对于虚拟化:设备可能要安装在 ...