Entity Framework Proxy creation

你。 提交于 2019-12-03 07:08:30

问题


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 ?


回答1:


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.



回答2:


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.



来源:https://stackoverflow.com/questions/6198563/entity-framework-proxy-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!