EF6 Working with Proxies ProxyCreationEnabled_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > EF6 Working with Proxies ProxyCreationEnabled

EF6 Working with Proxies ProxyCreationEnabled

 2014/8/13 15:24:29  Celery  程序员俱乐部  我要评论(0)
  • 摘要:WhencreatinginstancesofPOCOentitytypes,theEntityFrameworkoftencreatesinstancesofadynamicallygeneratedderivedtypethatactsasaproxyfortheentity
  • 标签:
When creating instances of POCO entity types, the Entity Framework often creates instances of a dynamically generated derived type that acts as a proxy for the entity. This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed. 代理类重写了virtual属性 For example, this mechanism is used to support lazy loading of relationships. The techniques shown in this topic apply equally to models created with Code First and the EF Designer. 在lazy loading中会用到 Most of the time you don't need to be aware of this use of proxies, but there are exceptions: 序列化的时候代理类就不能用了 1. In some scenarios you might want to prevent the Entity Framework from creating proxy instances. For example, when you're serializing entities you generally want the POCO classes, not the proxy classes. One way to avoid serialization problems is to serialize data transfer objects (DTOs) instead of entity objects, as shown in the Using Web API with Entity Framework tutorial. Another way is to disable proxy creation. 2. When you instantiate an entity class using the new operator, you don't get a proxy instance. This means you don't get functionality such as lazy loading and automatic change tracking. This is typically okay; you generally don't need lazy loading, because you're creating a new entity that isn't in the database, and you generally don't need change tracking if you're explicitly marking the entity as Added. However, if you do need lazy loading and you need change tracking, you can create new entity instances with proxies using the Create method of the DbSet class. 3. You might want to get an actual entity type from a proxy type. You can use the GetObjectType method of the ObjectContext class to get the actual entity type of a proxy type instance.
  • 相关文章
发表评论
用户名: 匿名