13

In your DbContext you can configure the following two parameters:

context.Configuration.ProxyCreationEnabled = true;
context.Configuration.LazyLoadingEnabled = true;

My understanding is that to enable lazy loading you have to be able to create proxies for the entities. In other words both parameters need to be set to true to enable lazy loading.

1. Why do both parameters exist and why can you configure both parameters?

2. What would the effect of the following configurations be?

// Can't create proxies but can lazy load
context.Configuration.ProxyCreationEnabled = false;
context.Configuration.LazyLoadingEnabled = true;

// Can create proxies but can't lazy load
context.Configuration.ProxyCreationEnabled = true;
context.Configuration.LazyLoadingEnabled = false;
  • is http://stackoverflow.com/questions/4596371/what-are-the-downsides-to-turning-off-proxycreationenabled-for-ctp5-of-ef-code-f the answer ? – tschmit007 Sep 08 '14 at 08:26

1 Answers1

10

AFAIK:

  • proxy creation true and lazy loading true =>
    • change tracking
    • lazy loading
  • proxy creation true and lazy loading false =>
    • change tracking
  • proxy creation false and lazy loading true =>
    • ...

reference (among others): msdn

tschmit007
  • 7,559
  • 2
  • 35
  • 43
  • Thanks for your answer. How do proxies change the way change tracking works? –  Sep 08 '14 at 08:32
  • 2
    with no proxy, no tracking. So `context.SaveChanges()` will never do anything. – tschmit007 Sep 08 '14 at 08:34
  • I think change tracking has nothing to do with the proxy, basically as far as my understanding both of them are used together to enable lazy loading, enable proxy only has no benefit, unless you can give specific code sample that explain that, and I just tried disabled both, `Find(1)`, change the prop, then `SaveChanges`, do save the changes – Yuliam Chandra Sep 08 '14 at 09:05
  • I just found this article [link](http://blogs.msdn.com/b/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx). It appears change tracking will still work it just works in a different way –  Sep 08 '14 at 09:13
  • you are right, `SaveChanges()` does the job, but the context has to evaluate the changes and can't give them to you before the save, neither after as the entity state stays unchanged. – tschmit007 Sep 08 '14 at 09:24
  • 1
    Proxies are needed for tracking because they are meant to replace the real entity instance with a proxy that contains the entity and some extra properties that allow the tracking: mainly the `State` property, with values `Added`, `Unchanged`, etc. Without these properties EF is missing important information to figure out what to persist, so `SaveChanges` doesn't always work as expected (it is not that it doesn't work at all, new entities are still detected and saved, but other changes can be missed and not persisted). – Diana Jul 03 '17 at 20:47