博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework Tutorial Basics(38):Explicit Loading
阅读量:6330 次
发布时间:2019-06-22

本文共 1687 字,大约阅读时间需要 5 分钟。

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

转载于:https://www.cnblogs.com/purplefox2008/p/5649436.html

你可能感兴趣的文章
PHP实现排序算法
查看>>
Business Contact Mnanager for Outlook2010
查看>>
9种用户体验设计的状态是必须知道的(五)
查看>>
解决WIN7下组播问题
查看>>
陈松松:视频营销成交率低,这三个因素没到位
查看>>
vmware nat模式原理探究,实现虚拟机跨网段管理
查看>>
JavaSE 学习参考:类的静态成员和静态方法
查看>>
JavaSE 学习参考:集合运算
查看>>
CSS属性:font-family
查看>>
【Signals and Systems】 SYLLABUS
查看>>
RH135-2-command-line-interface
查看>>
浅谈OS
查看>>
mac下开启docker API远程调用
查看>>
tar 命令的详解
查看>>
Cisco路由器安全配置
查看>>
第十次作业
查看>>
spring事务管理(一)
查看>>
给定一个字符串s,返回去掉子串"mi"后的字符串。
查看>>
Nginx 外的另一选择,轻量级开源 Web 服务器 Tengine 发布新版本
查看>>
配置免密码登录Linux服务器
查看>>