EF 4 - Lazy Loading Without Proxies
Asked Answered
D

1

14

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?

Doughnut answered 13/3, 2012 at 17:26 Comment(0)
B
11

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.

Breakup answered 13/3, 2012 at 18:11 Comment(4)
I will keep in mind that EntityObject is a "legacy entity" :) When do you start to mark it with the [Obsolete] attribute?Abscess
Good question. Which I can't answer because I don't know if and when it will ever happen. But we have discussed it.Breakup
@ajcvickers, thanks for your reply, but, unless I don´t understand correctly, I think was missed the most important part of the question: if I´m using ObjectContext (and not DbContext), what the ProxyCreationEnabled is used for? To enable Change Track? How I´ve stated before, this is not for Lazy Loading, so, what is it for?Doughnut
@rperson If EF could create a proxy for an object, then it will do so unless ProxyCreationEnabled is set to false, in which case it won't. Are you really asking what proxies are and how they work, or what this flag is for? If it is the former, then check out: blog.oneunicorn.com/2011/12/05/…Breakup

© 2022 - 2024 — McMap. All rights reserved.