简体   繁体   English

谁能解释实体框架中的延迟加载?

[英]Can anyone explain Lazy loading in Entity Framework?

I want a simple explanation about Lazy loading in Entity Framework with a simple example. 我想通过一个简单的示例对Entity Framework中的延迟加载进行简单说明。

  • Why do we use Lazy loading? 为什么我们使用延迟加载?
  • When do we use Lazy loading? 什么时候使用延迟加载?

Lazy loading means delaying the loading of related data until you specifically request it. 延迟加载意味着延迟相关数据的加载,直到您明确要求为止。

take an example from here.. 从这里举一个例子。

Lazy loading 延迟加载

As stated by Mike Stone in this 正如Mike Stone在此所述

It's called lazy loading because, like a lazy person, you are putting off doing something you don't want to. 之所以称其为“延迟加载”,是因为像一个懒惰的人一样,您推迟执行不想做的事情。 The opposite is Eager Loading, where you load something right away, long before you need it. 与之相反的是“急切加载”,您可以在需要时就立即加载某些东西。

If you are curious why people might use lazy loading, consider an application that takes a LOOOOONG time to start. 如果您好奇为什么人们可能会使用延迟加载,请考虑一个需要花费很长时间启动的应用程序。 This application is probably doing a lot of eager loading... loading things from disk, and doing calculations and whatnot long before it is ever needed. 这个应用程序可能正在做很多急切的工作……从磁盘上装载东西,并进行计算等等,直到需要它之前。

Compare this to lazy loading, the application would start much faster, but then the first time you need to do something that requires some long running load, there may be a slight pause while it is loaded for the first time. 将此与延迟加载进行比较,应用程序将启动得更快,但是,当您第一次需要执行需要长时间运行的加载的操作时,首次加载时可能会稍有暂停。 Thus, with lazy loading, you are amortizing the load time throughout the course of running your application... and you may actually save from loading things that the user may never intend to use. 因此,通过延迟加载,您可以在运行应用程序的整个过程中分摊加载时间……实际上,您可以避免加载用户永远不会使用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM