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 ...
随机推荐
- 如何用nodejs启一个前端服务
1.新建文件夹,如 notice 2.新建页面和js文件,如 index.html server.js 3.index.html页面内容随你写,如: <!DOCTYPE html> < ...
- SQL Server 学习系列之五
SQL Server 学习系列之五 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题) SQL Server 学习系列之三(SQL 关键字) SQ ...
- [转]你知道用AngularJs怎么定义指令吗?--很详细
前言 最近学习了下angularjs指令的相关知识,也参考了前人的一些文章,在此总结下. 欢迎批评指出错误的地方. Angularjs指令定义的API AngularJs的指令定义大致如下 ang ...
- 第一章计算机网络和因特网-day01
什么是因特网: 其一:构成因特网的基本硬件与软件. 其二:为分布式应用提供服务的联网基础设施. 终端机器称为主机( host ) 或者端系统( end system ) 端系统通过通信链路(commu ...
- sourcetree 安装与操作
sourcetree操作 http://www.jianshu.com/p/be9f0484af9d SourceTree安装教程和GitLab配置详解 http://www.cnblogs.com/ ...
- POJ2236(并查集入门)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 22977 Accepted: 961 ...
- php用zendstudio建立wsdl
首先,新建的时候要选择soap,然后deocument和rpc都可以. 类和方法的页面: <?php //发货接口 class test{ function send_do_delivery($ ...
- 非常不错的LTE架构讲解
<LTE系统协议架构---通俗易懂超经典> <3GPP协议导读> <3GPP协议36211-850中文翻译> <LTE全套协议汇总> <NB-IO ...
- mysql函数之五:group_concat mysql 把结果集中的一列数据用指定分隔符转换成一行
函数使用说明:该函数返回带有来自一个组的连接的非NULL 值的字符串结果.其完整的语法如下 GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {un ...
- 设置android的versionCode
在config.xml里面设置 android-versionCode="1" AndroidManifest.xml 将会修改 android:versionCode=" ...