I'm having trouble with entity framework returning Proxies when I want the actual entity class. The first time I run my code everything runs properly (no proxies), but every iteration afterwards one of my DbSets always returns proxies instead of the actual type.
I dispose of the context after every iteration, so I don't understand why the first time through it works, and every time after doesn't.
My code fails on this line. All my POCOs have the Table attribute set, but because it is returning a proxy class there is no table attribute.
TableAttribute attrib = (TableAttribute)attributes.Single();
Is there some behind the scenes static magic in the DbContext that lives after I destroy the object?
I move my objects into memory using the following
MajorClasses = ctx.MajorClasses.ToArray();
I also tried
MajorClasses = ctx.MajorClasses.AsNoTracking().ToArray();
In my OnModelCreating I have the following set
base.Configuration.ProxyCreationEnabled = false; base.Configuration.LazyLoadingEnabled = false;
Whenever a property which is mapped to the database is accessed, the proxy subclass will carry out the load from the database, so that the load is transparent to the client code. Proxies are typically created when you have a relationship property between two entities which is lazily loaded.
Proxies. Proxies are objects deriving from your entities that are generated at runtime by Entity Framework Core. These proxies have behaviour added to them that result in database queries being made as required to load navigation properties on demand.
ProxyCreationEnabled is set to true , child objects will be loaded automatically, and DbContext. Configuration. LazyLoadingEnabled value will control when child objects are loaded.
You can set ObjectContext.ContextOptions.ProxyCreationEnabled
to false. This will prevent you from using some of EFs fancy features like lazy loading and I believe change tracking.
As far as your app cares, it should be able to treat the proxies just like the types they represent. Is there a specific issue you are having?
Edit
We have some code that requires the POCO type instead of the proxy type and we do the following to detect if the current type is a proxy.
if (entityType.BaseType != null && entityType.Namespace == "System.Data.Entity.DynamicProxies") { entityType = entityType.BaseType; }
To turn off proxy creation in Entity Framework 5 you can use the following,
_dbContext.Configuration.ProxyCreationEnabled = false;
Simply set this property once before using the context to pull data.
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