延迟加载VS预先加载 [英] Lazy Loading vs Eager Loading

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

问题描述

延迟加载是发生加载和访问相关实体的默认的现象。然而,急于加载提到了力加载所有这些关系的做法。我所遇到的,在什么情况下预先加载可能比懒加载更为有利的问题。问这个,因为这是明显的延迟加载更多的资源友好的,即使我们使用了了ToList()方法,我们还可以采取延迟加载行为的优势。不过,我想也许延迟加载增加请求到实际的数据库的数量,也许这就是为什么有时候开发者使用 Inlcude 方法强制加载的所有关系。例如,在MVC 5使用Visual Studio自动脚手架的时候,那是在控制器自动创建索引方法总是使用预先加载,而且我一直有,为什么微软在这种情况下,使用预先加载默认的问题。

Lazy loading in Entity Framework is the default phenomenon that happens for loading and accessing the related entities. However, eager loading is referred to the practice of force-loading all these relations. I have come across the question of under what situation eager loading could be more beneficial than lazy loading. Asking this, because it is obvious that lazy loading is more resource friendly, and even if we use the ToList() method, we can still take advantage of the lazy loading behavior. However, I thought maybe lazy loading increases the number of requests to the actual database and maybe that's why sometimes developers use the Inlcude method to force-loading all relations. For example, when using the Visual Studio auto-scaffolding in MVC 5, the Index method that is automatically created in the controller always uses Eager Loading, and I've always had the question of why Microsoft uses Eager Loading by default in that case.

如果有人在什么样的情况下预先加载会比懒加载更有利于解释给我,我将不胜感激,以及为什么我们使用它的同时还能有更多的东西资源友好如延迟加载。

I would appreciate if someone explains to me under what situation eager loading would be more beneficial than lazy loading, and why do we use it at all while there's something more resource friendly as Lazy Loading.

推荐答案

我觉得这是很好的分类,这样的关系。

I think it is good to categorize relations like this

何时使用预先加载


  1. 在一面的一个一对多的关系,你肯定是使用每一个地方与主要实体。喜欢的文章的用户属性。一个产品类别属性。

  2. 一般当关系不是太大,渴望负荷将是很好的做法,以减少对服务器进一步查询。

何时使用延迟加载


  1. 差不多就一到每一个集方 -many关系。像用户的文章或产品类别

  2. 你确切地知道,你不会需要一个属性瞬间。

注意:像超然说,有可能是处置问题,延迟加载

Note: like Transcendent said there may be disposal problem with lazy loading.

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

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