Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4 - Lazy Loading Without Proxies

I´ve read that proxies are used when wee need to use Lazy Loading and Change Tracking. In other words, to use Lazy Loading I must enable proxies.

So far so good.

the point is that I can use the code bellow to setup the context to not use a proxy and even yet use lazy loading.

ctx = new SchoolEntities();
ctx.ContextOptions.ProxyCreationEnabled = false;
ctx.ContextOptions.LazyLoadingEnabled = true;

Is the ProxyCreationEnabled property related only to change tracking proxy or am I missing something?

Could someone please explain this with some details?

Thanks!

EDIT1

I´m not using POCO/DbContext. I´m using a regular edmx EF model with ObjectContext. I know the importance of proxies for POCO entities regards to change tracking and lazy loading. By why to use Proxies in a regular EDMX model?

like image 750
outlookrperson Avatar asked Mar 13 '12 17:03

outlookrperson


People also ask

Is lazy loading enabled by default in EF core?

The advice is not to use lazy loading unless you are certain that it is the better solution. This is why (unlike in previous versions of EF) lazy loading is not enabled by default in Entity Framework Core.

How do I set lazy loading in Entity Framework?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

How do I stop EF from lazy loading?

To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false. Rules for lazy loading: context.

Does Entity Framework support lazy loading?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.


1 Answers

When using POCO entities with the built-in features of Entity Framework, proxy creation must be enabled in order to use lazy loading. So, with POCO entities, if ProxyCreationEnabled is false, then lazy loading won't happen even if LazyLoadingEnabled is set to true.

With certain types of legacy entities (notably those the derive from EntityObject) this was not the case and lazy loading would work even if ProxyCreationEnabled is set to false. But don't take that to mean you should use EntityObject entities--that will cause you more pain.

The ProxyCreationEnabled flag is normally set to false when you want to ensure that EF will never create a proxy, possibly because this will cause problems for the type of serialization you are doing.

The LazyLoadingEnabled flag is normally used to control whether or not lazy loading happens on a context-wide basis once you have decided that proxies are okay. So, for example, you might want to use proxies for change tracking, but switch off lazy loading.

like image 58
Arthur Vickers Avatar answered Sep 18 '22 05:09

Arthur Vickers