POCO/PROXY 禁用延迟加载
在“Entity Framework 4 in Action”一书中,第 16.1.3 节并在 16.2.4 中再次指出
如果你有代理实例,记得先禁用延迟加载 序列化,否则您最终将向客户端发送不需要的数据。
然而,作者从未说过如何禁用延迟加载。在我自己的搜索中,我听说过 ContextOptions.LazyLoadingEnabled。这是完成此操作的“方式”吗?是否有不止一种方法(除了禁用代理生成之外)?
我读到 一篇文章指出 LazyLoadingEnabled 标志仅适用于 EntityObject 实体,而不适用于 POCO 实体:
关闭延迟加载现在将允许您的类被序列化 适当地。请注意,这是如果您使用标准实体 框架类。如果您使用 POCO,则必须执行以下操作 略有不同的东西。
使用 POCO,实体框架将默认创建代理类 允许诸如延迟加载之类的东西与 POCO 一起使用。这个代理 基本上创建一个代理对象,它是一个完整的实体框架 位于上下文和 POCO 对象之间的对象。使用时 POCO 与 WCF(或任何序列化)只是关闭延迟加载 没有削减它。您必须关闭代理创建以确保 您的类将正确序列化
我怀疑上面的评论完全是错误的。
In the book "Entity Framework 4 in Action", in section 16.1.3 and again in 16.2.4, it is stated
If you have a proxy instance, remember to disable lazy loading before
serializing, or you’ll end up sending unwanted data to the client.
However, the authors never say how to disable lazy loading. In my own searches, I've heard of ContextOptions.LazyLoadingEnabled. Is that "the way" this is done? Is there more than one way (besides disabling proxy generation)?
I read in one article that the LazyLoadingEnabled flag only pertains to EntityObject entities, not POCO entites:
Turning lazy loading off will now allow your classes to be serialized
properly. Note, this is if you are using the standard Entity
Framework classes. If you are using POCO, you will have to do
something slightly different.With POCO, the Entity Framework will create proxy classes by default
that allow things like lazy loading to work with POCO. This proxy
basically creates a proxy object that is a full Entity Framework
object that sits between the context and the POCO object. When using
POCO with WCF (or any serialization) just turning off lazy loading
doesn’t cut it. You have to turn off the proxy creation to ensure
that your classes will serialize properly
I suspect the above commentary is simply erroneous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,ContextOptions.LazyLoadingEnabled 就是“方法”。序列化问题相关,但有所不同。代理具有不同的运行时类型。即可以进行序列化。与延迟加载的唯一关系是纯(非代理)POCO 不执行延迟加载。
Yes,
ContextOptions.LazyLoadingEnabled
is "the way." The serialization issue is related, but different. Proxies have a different runtime type. That can mung serialization. The only relation to lazy loading is that pure (non-proxy) POCOs don't do lazy loading.