问题描述
我的 WCF 服务可以从代码优先模型返回类的唯一方法是使用以下代码将 ProxyCreationEnable
设置为 false
.
The only way that my WCF service can return classes from a code first model is by setting the ProxyCreationEnable
to false
using the code below.
((IObjectContextAdapter)MyDb).ObjectContext.ContextOptions.ProxyCreationEnable = false;
这样做的负面后果是什么?一个好处是我至少可以将这些动态类型序列化,以便可以使用 WCF 通过网络发送它们.
What are the negative consequences of doing this? One gain is that I can at least get these dynamic types serialized so they can be sent over the wire using WCF.
推荐答案
动态代理用于更改跟踪和延迟加载.当 WCF 尝试序列化对象时,相关的上下文通常是关闭和处理的,但导航属性的序列化将自动触发延迟加载(在关闭的上下文中)=> 异常.
Dynamic proxies are used for change tracking and lazy loading. When WCF tries to serialize object, related context is usually closed and disposed but serialization of navigation properties will automatically trigger lazy loading (on closed context) => exception.
如果您关闭延迟加载,则需要对要使用的所有导航属性使用预先加载(包括在 ObjectQuery 中).跟踪更改不适用于 WCF,它仅适用于修改附加到 ObjectContext 的实体.
If you turn off lazy loading you will need to use eager loading for all navigation properties you want to use (Include on ObjectQuery). Tracking changes doesn't work over WCF it works only for modification of entity which is attached to ObjectContext.
这篇关于首先关闭EF代码的CTP5的ProxyCreationEnabled有什么缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!