延迟加载,延迟加载和加载实体框架 [英] Lazy loading, Deferred Loading and Eager Loading in Entity framework

查看:104
本文介绍了延迟加载,延迟加载和加载实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这3种类型的加载有什么区别?有人可以举例说明吗?不同的资源在线使用不同的定义导致比必要的更多的混乱。

解决方案

懒惰加载和延迟是非常同义的(AFAIK,请更正我如果我错了)。 Eager和Lazy之间有很大的区别。渴望会发生在前面,懒惰只会根据需要发生,执行会发生在DB级别 - 我们以简单的JOIN语句为例。

  var people =(from p in people SELECT p).ToList(); 
var jobs =(from j in jobs SELECT j).ToList();

var peopleAndJobs =(from p in people JOIN j on j.personId equals p.personId SELECT p).ToList()

这是一个渴望加载的例子。我们得到所有的人,所有的工作,我们正在加入记忆。不是很聪明(通常)。这就是Lazy风格的样子。

  var people =(from p in people SELECT p); 
var jobs =(from j in jobs SELECT j);

var peopleAndJobs =(from p in people JOIN j on j.personId equals p.personId SELECT p).ToList()

这是做什么是为人和工作创建一个IQueryable(IQueryable是懒惰的),并且连接正在DB中发生。这可以节省网络活动,并且通常实际上更快,因为数据库被优化以进行连接等。



除非我们明确地说我需要数据! (通过ToListing它,迭代它等)它是懒惰的。还有一些怪癖,但这应该是一个体面的底线。


What are the differences between these 3 types of loading? Can someone explain with an example? Different resources online use different definitions causing more confusion than necessary.

解决方案

Lazy loading and Deferred are pretty synonymous (AFAIK, please correct me if I'm wrong). The big difference is between Eager and Lazy. Eager will happen up front, Lazy happens only "as needed" and execution will happen at the DB level- let's take a simple JOIN statement as an example

var people = (from p in people SELECT p).ToList();
var jobs = (from j in jobs SELECT j).ToList();

var peopleAndJobs = (from p in people JOIN j on j.personId equals p.personId SELECT p).ToList()

This is an example of eager loading. We are getting ALL people, ALL jobs, and we are doing the join in memory. Not very smart (usually). Here is what it looks like Lazy-style.

var people = (from p in people SELECT p);
var jobs = (from j in jobs SELECT j);

var peopleAndJobs = (from p in people JOIN j on j.personId equals p.personId SELECT p).ToList()

What this is doing is creating an IQueryable for both people and job (IQueryable is lazy), and the join is happening in the DB. This saves network activity, and is usually actually faster, since the DB is optimized to do joins and such.

Unless we explicitly say "I need that data!" (by ToListing it, iterating through it, etc) it's lazy. There are some more quirks but this should be a decent primer.

这篇关于延迟加载,延迟加载和加载实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆