简体   繁体   English

实体框架虚拟属性

[英]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实体的延迟加载支持,您需要做两件事:

  1. 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关系的引用。

  2. 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.

相关问题 实体框架属性中的虚拟关键字 - Virtual keyword in Entity Framework properties Entity Framework Core 6:属性虚拟与非虚拟 - Entity Framework Core 6 : properties virtual vs non virtual 实体框架虚拟属性和Id属性的数据库访问? - Entity Framework virtual properties and database access for Id properties? virtual关键字对Entity Framework 5中的实体属性有什么影响? - What effect does the virtual keyword have on entity properties in Entity Framework 5? 为什么在实体框架模型定义中对类属性使用“虚拟”? - Why use 'virtual' for class properties in Entity Framework model definitions? 实体框架 - Include()加载所有chidren属性,甚至是虚拟属性 - Entity framework - Include() loads all chidren properties even virtual ones 实体框架:为什么使用虚拟关键字在DbContext中声明属性 - Entity Framework: Why virtual keyword used to declare properties in DbContext 实体框架4.1:创建新实体后如何访问虚拟实体属性? - Entity Framework 4.1: How can I access virtual entity properties after creating a new entity? 实体框架 - 虚拟财产 - Entity Framework - Virtual Property 计算属性和实体框架 - Computed Properties and Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM