In Entity Framework 4 Is it possible to choose to load some queries into a POCO without it using proxy classes? (For the purpose of caching that object for future read only use). I am using the Repository - Service pattern.
By this I mean:
var order = _orderService.GetById(1);
// after order is loaded then we can see in the debugger that:
// order.Customer is of type System.Data.Entity.DynamicProxy.Customer_17631AJG_etc
What I want is for the order.Customer
to actually use the POCO type MyApp.Models.Entities.Customer
instead of a proxy to that type.
EDIT: Based on Ladislav's suggestion to add a "GetUnproxied" method to the Repository, I've made this change:
// this is the current method that must return a DynamicProxy
public IQueryable<T> GetQuery()
{
return ObjectSet.AsQueryable();
}
// this is the new additional method that must return the plain POCO
public IQueryable<T> GetReadOnly()
{
ObjectContext.ContextOptions.ProxyCreationEnabled = false;
var readOnly = ObjectSet.AsQueryable();
ObjectContext.ContextOptions.ProxyCreationEnabled = true;
return readOnly;
}
Is this correct?
It does not look thread safe to me. Both methods use the same ObjectContext instance, so it might be possible for ProxyCreationEnabled == false
to happen on one thread and then public IQueryable<T> GetQuery()
to be called on another thread - which would suddenly mean that the proxy method could return the non proxied object.
Use this before you query data to turn off proxy creation
context.ContextOptions.ProxyCreationEnabled = false;
I think it can be also turned off globally in EDMX designer.
This applied to ObjectContext
. With DbContext
the code is:
context.Configuration.ProxyCreationEnabled = false;
plus I do not see any option in the edmx designer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With