什么是延迟加载和负载之间的差异() [英] What is the difference between Lazy Loading and Load()

查看:810
本文介绍了什么是延迟加载和负载之间的差异()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架4中,Lazy加载和使用Load()方法有什么区别?

In Entity Framework 4, what is the difference between Lazy Loading, and using Load() method?

编辑: 'statements:

延迟加载:

var query = from c in context.Contacts select c;
foreach ( var contact in query ) {
     if ( contact.ID == 5 )
        Console.WriteLine( contact.Addresses.City );
}

Load()方法:

context.ContextOptions.LazyLoadingEnabled = false;

var query = from c in context.Contacts select c;
foreach ( var contact in query ) {
     if ( contact.ID == 5 ) {
        contact.Addresses.Load()
        Console.WriteLine( contact.Addresses.City );
     }
}

现在,有了这两个'if'

Now, having this two 'if' checks, why should I preffer one before another?

推荐答案

延迟加载意味着

当您禁用延迟加载时,您将不会加载不必要的数据。说您将通过调用load来加载自己。

When you disable Lazy Loading you say that you will load yourself by calling load.

http://en.wikipedia.org/wiki/Lazy_loading

延迟载入已停用默认,所以当你在第一行将它设置为 false 它不做任何事情。

Lazy Loading is disabled by default, so when you set it to false in your first line it does not do anything.

当您调用 Load 时,您将加载所有相关对象到该数据库case使它没有它工作)

When you call Load, you will load all the related objects to that database (which is not needed in this case which makes it work without it)

这篇关于什么是延迟加载和负载之间的差异()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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