What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first
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;
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.
If DbContext.Configuration.ProxyCreationEnabled
is set to false
, DbContext will not load child objects for some parent object unless Include
method is called on parent object. Setting DbContext.Configuration.LazyLoadingEnabled
to true
or false
will have no impact on its behaviours.
If DbContext.Configuration.ProxyCreationEnabled
is set to true
, child objects will be loaded automatically, and DbContext.Configuration.LazyLoadingEnabled
value will control when child objects are loaded.
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.
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.