EF code first - getting DynamicProxies instead of objects. What for?

I have the following query:

using (var forumsDb = new ForumsDb()) { forumsDb.Configuration.LazyLoadingEnabled = false; var categoryList = forumsDb.Categories.Select(c => c).ToList(); /***some code here***/ } 

Now the categoryList contains 4 elements (I have 4 categories in the database), but all of them are of type dynamicproxies , and not (as expected) Category .

What am I doing wrong?

+4
source share
2 answers

You are not doing anything wrong. EF wraps your objects in a version tracking proxy so that it can detect changes in your objects and also support Lazy Loading.

If you want to remove the proxy, you can disconnect the object from the context or disable version tracking altogether by specifying DbContext.Configuration.ProxyCreationEnabled false

+5
source

Dynamic proxies automatically create wrappers around your objects that handle change tracking to ensure that the correct objects are saved when SaveChanges starts.

Inherit from your objects (in your case Category ) and can be used as the corresponding object.

+1
source

Source: https://habr.com/ru/post/1434685/


All Articles