EF 4 - 延迟加载,无需代理 [英] EF 4 - Lazy Loading Without Proxies

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

问题描述

从来就读取代理用于凌晨时需要使用延迟加载和变更跟踪。换句话说,使用延迟加载我必须启用代理服务器。

I´ve read that proxies are used when wee need to use Lazy Loading and Change Tracking. In other words, to use Lazy Loading I must enable proxies.

到目前为止好。

的一点是,我可以用code波纹管设置的背景下不使用代理,甚至还使用延迟加载。

the point is that I can use the code bellow to setup the context to not use a proxy and even yet use lazy loading.

ctx = new SchoolEntities();
ctx.ContextOptions.ProxyCreationEnabled = false;
ctx.ContextOptions.LazyLoadingEnabled = true;

时只有改变跟踪代理还是我失去了一些东西?相关ProxyCreationEnabled财产

Is the ProxyCreationEnabled property related only to change tracking proxy or am I missing something?

可能有人请一些细节解释一下吗?

Could someone please explain this with some details?

谢谢!

EDIT1

我可不是使用POCO /的DbContext。 I'm使用带有ObjectContext的常规EDMX EF模型。我知道代理的POCO实体的重要性,认为改变跟踪和懒加载。通过为什么要在常规EDMX模型中使用代理?

I´m not using POCO/DbContext. I´m using a regular edmx EF model with ObjectContext. I know the importance of proxies for POCO entities regards to change tracking and lazy loading. By why to use Proxies in a regular EDMX model?

推荐答案

在使用POCO实体与实体框架的内置功能,代理的创建必须要使用延迟加载功能。所以,用POCO实体,如果ProxyCreationEnabled是假的,那么延迟加载不会即使LazyLoadingEnabled设置为true发生。

When using POCO entities with the built-in features of Entity Framework, proxy creation must be enabled in order to use lazy loading. So, with POCO entities, if ProxyCreationEnabled is false, then lazy loading won't happen even if LazyLoadingEnabled is set to true.

使用某些类型的传统实体(尤其是那些从该派生EntityObject),这是不是这样,如果ProxyCreationEnabled设置为false,延迟加载甚至会工作。但不要认为这意味着你应该使用EntityObject实体 - 这会导致你更痛苦。

With certain types of legacy entities (notably those the derive from EntityObject) this was not the case and lazy loading would work even if ProxyCreationEnabled is set to false. But don't take that to mean you should use EntityObject entities--that will cause you more pain.

当你想确保EF永远不会创建一个代理,可能是因为这将导致问题进行序列化的你正在做的类型ProxyCreationEnabled标志通常设置为false。

The ProxyCreationEnabled flag is normally set to false when you want to ensure that EF will never create a proxy, possibly because this will cause problems for the type of serialization you are doing.

该LazyLoadingEnabled标志通常用于控制延迟加载发生是否上下文范围的基础上,一旦你已经决定代理都还好。因此,例如,您可能希望使用更改跟踪代理,但关闭延迟加载。

The LazyLoadingEnabled flag is normally used to control whether or not lazy loading happens on a context-wide basis once you have decided that proxies are okay. So, for example, you might want to use proxies for change tracking, but switch off lazy loading.

这篇关于EF 4 - 延迟加载,无需代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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