[英]Entity Framework Virtual Properties
What is the effect of not marking navigation properties virtual for Entity Framework (v4) entities? 不为Entity Framework(v4)实体标记虚拟导航属性的影响是什么?
If I don't use lazy loading, but neglect to properly eager load, what will happen upon accessing the (not loaded) property? 如果我不使用延迟加载,但忽略了正确的加载,访问(未加载)属性会发生什么?
Would there be any other impact outside of lazy loading, or is that the main feature that requires virtual navigation properties? 是否会在延迟加载之外产生任何其他影响,或者是需要虚拟导航属性的主要功能?
There are two effects. 有两种效果。 One is on lazy loading, which just won't work if the property is not
virtual
. 一个是延迟加载,如果属性不是
virtual
,它将无法工作。 But lazy loading isn't always something you want. 但延迟加载并不总是你想要的。
The second is change detection. 第二是变化检测。 If you're using pure POCOs or are using proxies but don't make the properties
virtual
, then change detection becomes more involved. 如果您正在使用纯POCO或使用代理但不使
virtual
属性,则更改检测变得更加复杂。
You can learn more about change detection here . 您可以在此处详细了解更改检测。
If you are wondering whether Deferred Loading is supported with POCO objects, then I think you will be happy to know that you can get Deferred Loading with POCO as well. 如果您想知道POCO对象是否支持延迟加载,那么我想您很高兴知道您也可以通过POCO获得延迟加载。
There are two things you need to do in order to get Deferred Loading support with POCO entities: 为了获得POCO实体的延迟加载支持,您需要做两件事:
Declare the property that you would like to load lazily as virtual. 声明要将懒惰加载为虚拟的属性。 These properties can be any collection type that implements ICollection or they can be a reference representing a 1/0..1 relationship.
这些属性可以是实现ICollection的任何集合类型,也可以是表示1 / 0..1关系的引用。
Enable deferred loading on the context 在上下文中启用延迟加载
See MSDN's "POCO in the Entity Framework" post . 请参阅MSDN的“实体框架中的POCO”帖子 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.