Entity Framework Tutorial Basics(38):Explicit Loading
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的更多相关文章
- 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(36):Eager Loading
Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...
- 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 ...
随机推荐
- msyql中myism和innodb的区别
MyISAM存储引擎 MyISAM是 默认存储引擎.它基于更老的ISAM代码,但有很多有用的扩展.MyISAM存储引擎的一些特征: ●所有数据值先存储低字节.这使得数据机和操作系统分离.二进制轻便性的 ...
- @media screen页面自适应尺寸!!
@media screen and (max-width:360px){body,input,select{font-size:38%}} @media screen and (min-width:3 ...
- LeetCode Predict the Winner
原题链接在这里:https://leetcode.com/problems/predict-the-winner/description/ 题目: Given an array of scores t ...
- LeetCode Largest Palindrome Product
原题链接在这里:https://leetcode.com/problems/largest-palindrome-product/description/ 题目: Find the largest p ...
- Node中没搞明白require和import,你会被坑的很惨
ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...
- Process使用
最近在一个项目中,需要在C#中调用cmd窗口来执行一个命令,使用到了Process这个类,使用过程中遇到不少问题,好在终于解决了.赶紧记录下来. Process process = new Proce ...
- Day2-Python基础2---列表、元组操作
一.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 names = ["maqing"," peilin" ...
- 开发环境入门 linux基础 (部分) 归档 压缩 Vi编译器 系统分区
归档 压缩 Vi编译器 系统分区 1.使用cat命令进行文件的纵向合并 1) 使用cat命令实现文件的纵向合并: a) 例如:将用户信息数据库文件和组信息数据库文件 ...
- 第三章 深入分析Java Web的中文乱码问题(待续)
几种常见的编码格式 在Java中需要编码的场景 在Java中如何编解码 在Java Web中涉及的编解码 在JS中的编码问题 常见问题分析 一种繁简转换的实现方式
- Linux 命令及获取帮助 目录文件浏览,管理和维护
开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 $ 使用命令退出虚拟终端2上登录的用户 exit ...