Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Proxy creation

We can stop creation of proxy in the context constructor by using

this.Configuration.ProxyCreationEnabled = false;

What are the advantages and disadvantages of creating proxies in EF 4.1 ?

like image 627
Jayantha Lal Sirisena Avatar asked Jun 01 '11 08:06

Jayantha Lal Sirisena


People also ask

What is dynamic proxy entity framework?

Dynamic Proxy IT can also be said that it is a runtime proxy classes like a wrapper class of POCO entity. You can override some properties of the entity for performing actions automatically when the property is accessed. This mechanism is used to support lazy loading of relationships and automatic change tracking.

How do I disable POCO proxy creation using DB context?

ContextOptions. ProxyCreationEnabled property is true by default. You need to explicitly set it to "false" in the default constructor of your context object in order to turn this feature off.

What is ProxyCreationEnabled?

ProxyCreationEnabled is set to true , child objects will be loaded automatically, and DbContext. Configuration. LazyLoadingEnabled value will control when child objects are loaded.


2 Answers

Proxies are necessary for two features:

  • Lazy loading - navigation properties are loaded once accessed first time
  • Dynamic change tracking - if you modify any property in the entity the context is notified about this change and set the state of the entity. If dynamic change tracking is not used, context must use snapshot change tracking which means discovery all changes before saving takes place = exploring all properties even if they were not changed.

Both these techniques have other requirements:

  • Lazy loading - all navigation properties in the entity must be virtual. Lazy loading must be enabled.
  • Dynamic change tracking - all mapped properties must be virtual.
like image 193
Ladislav Mrnka Avatar answered Oct 06 '22 08:10

Ladislav Mrnka


In addition to Previous answer, Runtime use your POCO class using reflection and create a Dynamic Proxy class inheriting your POCO class. So it will add those functionalities + EntityObject functionalities in runtime that will help Dynamic proxies to enable Lazy Loading and Dynamic change Tracking.

like image 35
marvelTracker Avatar answered Oct 06 '22 08:10

marvelTracker