Explicit Loading with DBContext

Even with lazy loading disabled, it is still possible to lazily load related entities, but it must be done with an explicit call. Use the Load method of DBEntityEntry object to accomplish this.

The following code explicitly loads Standard of particular Student using the Reference() method of DbEntityEntry:

using (var context = new SchoolDBEntities())
{
//Disable Lazy loading
context.Configuration.LazyLoadingEnabled = false; var student = (from s in context.Students
where s.StudentName == "Bill"
select s).FirstOrDefault<Student>(); context.Entry(student).Reference(s => s.Standard).Load();
}

If you run the code shown above, you can see that it first loads student but not standard, as shown below:

The load method to get the Standard entity is shown below:

The code shown above will execute two different database queries. The first query gets Student and the second query gets Standard.

Load collection:

Use the Collection() method instead of Reference() method to load collection navigation property. The following example loads the courses of student.

using (var context = new SchoolDBEntities())
{
context.Configuration.LazyLoadingEnabled = false; var student = (from s in context.Students
where s.StudentName == "Bill"
select s).FirstOrDefault<Student>(); context.Entry(student).Collection(s => s.Courses).Load();
}

Note: The Load extension method works just like ToList, except that it avoids the creation of the list altogether.

Entity Framework Tutorial Basics(38):Explicit 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(36):Eager Loading

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

  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. 【SQL查询】查询的值为空时,给出默认值_NVL函数

    格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值. 引申一下,此NVL的作 ...

  2. Agc001_D Arrays and Palindrome

    传送门 题目大意 给定一个元素和为$N$的有$M$个数的序列$A$,请你可以$A$元素排列的顺序,并需要构造一个有$K$个($K$可以自己定)数的数列,使得任意一个长度为$N$的字符串,若满足:前$A ...

  3. 【LeetCode】075. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. jsp中引入JavaScript的方法

    1:在页面中直接嵌入JavaScript <script language="javascript">..........</script> 2:链接外部J ...

  5. php写入数据到mysql数据库中出现乱码解决方法

    乱码情况: 在选择数据库前加入一句代码即可 mysql_query("set names utf8"); 最后效果

  6. secret CRT 会话光标不闪烁问题

    点击 选项->会话选项 然后在取消即可,就有了闪烁的光标,应该是个bug.

  7. java基础练习。。replaceall

    总结:方法不是别人告诉你的.再与你的手 package com.bc; public class gdfk { public static void main(String[] args) { Str ...

  8. linux下mysql配置文件my.cnf最详细解释

    MySQL配置文件在Windows下叫my.ini,在MySQL的安装根目录下:在Linux下叫my.cnf,该文件位于/etc/my.cnf. 可以查找下:find / -name my.cnf m ...

  9. PostgreSQL recovery.conf恢复配置

    PostgreSQL recovery.conf恢复配置 这一章描述recovery.conf 文件中可用的设置.它们只应用于恢复期.对于你希望执行的任意后续恢复, 它们必须被重置.一旦恢复已经开始, ...

  10. 单片机RS485通信接口、控制线、原理图及程序实例

    RS232 标准是诞生于 RS485 之前的,但是 RS232 有几处不足的地方: 接口的信号电平值较高,达到十几 V,使用不当容易损坏接口芯片,电平标准也与TTL 电平不兼容. 传输速率有局限,不可 ...