简体   繁体   English

virtual关键字对Entity Framework 5中的实体属性有什么影响?

[英]What effect does the virtual keyword have on entity properties in Entity Framework 5?

I know that navigation properties in Entity Framework must have the virtual keyword in front of them or else EF can't do it's navigation property magic. 我知道实体框架中的导航属性必须在它们前面有虚拟关键字,否则EF不能做它的导航属性魔术。 But what effect does putting the virtual keyword in front of other properties have? 但是将虚拟关键字放在其他属性前面有什么影响呢? I saw an example recently wherein every single property on the entity was declared with the virtual keyword. 我最近看到一个例子,其中实体上的每个属性都是用virtual关键字声明的。 Does EF 5 really lazy load every property if I do that or am I missing something about how that works? 如果我这样做,EF 5是否真的懒得加载每个属性,或者我错过了一些关于它如何工作的东西?

Using virtual keyword for navigation properties is necessary for lazy loading. 对于延迟加载,必须使用虚拟关键字作为导航属性。 Using virtual keyword on all mapped properties is necessary for dynamic change tracking. 在动态更改跟踪中,必须在所有映射属性上使用虚拟关键字。 All requirements both options are described in documentation . 所有要求都在文档描述

In EF 4.0 dynamic change tracking was recommended approach and because of that POCO T4 Generator used virtual properties but since EF 4.1 snapshot change tracking is recommended as initial option with shift to dynamic change tracking only when shapshot change tracking is victim of performance issues. 在EF 4.0中,动态更改跟踪是推荐的方法,因为POCO T4生成器使用了虚拟属性,但由于EF 4.1 快照更改跟踪建议作为初始选项 ,只有当shapshot更改跟踪成为性能问题的受害者时,才会切换到动态更改跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM