Using proxy classes to load data from a database?

c# entity-framework poco proxy-classes


Is it feasible to load certain queries into a POCO in Entity Framework 4 without utilizing proxy classes? (In order to cache the item for read-only usage in the future. The Repository-Service pattern is what I'm using.

This is to say:

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

My goal is for theorder.Customer putting the POCO type to useMyApp.Models.Entities.Customer as opposed to a proxy for that kind.

EDIT: I've made the following adjustment in response to Ladislav's request to include a "GetUnproxied" function in the Repository:

// 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 accurate?

I don't think it seems to be thread safe. The same ObjectContext object is used by both methods, hence it may be feasible forProxyCreationEnabled == false must take place on one thread beforepublic IQueryable<T> GetQuery() be invoked on a different thread, at which point the proxy function would suddenly be able to return the original object.

1/5/2015 4:03:44 PM

Accepted Answer

Use this to stop proxy generation before performing a data query.

context.ContextOptions.ProxyCreationEnabled = false;

In EDMX designer, in my opinion, it may also be turned off worldwide.


This was true forObjectContext . WithDbContext the key is

context.Configuration.ProxyCreationEnabled = false;

Moreover, I cannot see any options in the edmx designer.

1/5/2015 3:33:42 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow