C#EF Code First虚拟关键字,它有什么作用?

时间:2021-12-15 02:10:49

Why exactly do we need to use the "virtual" keyword when declaring a navigation property? I understand that the Code First framework uses it somehow to recognize that the property is a navigation property, but I'd like to know how. Specifically, I'd like to know how it relates to the description given in the MSDN documentation for the "virtual" keyword: http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.80).aspx

在声明导航属性时,为什么我们需要使用“virtual”关键字?我知道Code First框架以某种方式使用它来识别属性是导航属性,但我想知道如何。具体来说,我想知道它与MSDN文档中关于“虚拟”关键字的描述有何关联:http://msdn.microsoft.com/en-us/library/9fkccyh4(v = vs.80)的.aspx

2 个解决方案

#1


13  

On runtime, Entity Framework will generate for you what's called proxy-entities. Those entities are objects of dynamically created types that derive from your entity types.

在运行时,Entity Framework将为您生成所谓的代理实体。这些实体是从您的实体类型派生的动态创建类型的对象。

This allows you to use your entities as a POCO, which is a simple object that is not related to Entity Framework in any way, as it doesn't inherit from EntityObject.

这允许您将实体用作POCO,这是一个与Entity Framework无关的简单对象,因为它不从EntityObject继承。

On runtime, the dynamically created entity type inherits from your POCO, and overrides all your virtual properties to add the Entity Framework stuff that allows lazy-loading in the properties getters.

在运行时,动态创建的实体类型继承自您的POCO,并覆盖所有虚拟属性以添加允许在属性getter中进行延迟加载的Entity Framework内容。

Lazy loading is a complex process that requires your code to know about how the data comes from the database. As you don't want your domain classes to know about the database and the EF stuff, you abstract your entities from EF and add virtual properties, so EF can override your base POCO and add its DB-related stuff on runtime.

延迟加载是一个复杂的过程,需要您的代码知道数据是如何来自数据库的。由于您不希望域类了解数据库和EF内容,因此您可以从EF中抽象实体并添加虚拟属性,因此EF可以覆盖基础POCO并在运行时添加与DB相关的内容。

Same for change tracking.

更改跟踪也是如此。

#2


1  

Adding virtual allows EF to generate a derived class that overrides the property and returns a set from the database.

添加虚拟允许EF生成一个派生类,该类覆盖该属性并从数据库返回一个集合。

#1


13  

On runtime, Entity Framework will generate for you what's called proxy-entities. Those entities are objects of dynamically created types that derive from your entity types.

在运行时,Entity Framework将为您生成所谓的代理实体。这些实体是从您的实体类型派生的动态创建类型的对象。

This allows you to use your entities as a POCO, which is a simple object that is not related to Entity Framework in any way, as it doesn't inherit from EntityObject.

这允许您将实体用作POCO,这是一个与Entity Framework无关的简单对象,因为它不从EntityObject继承。

On runtime, the dynamically created entity type inherits from your POCO, and overrides all your virtual properties to add the Entity Framework stuff that allows lazy-loading in the properties getters.

在运行时,动态创建的实体类型继承自您的POCO,并覆盖所有虚拟属性以添加允许在属性getter中进行延迟加载的Entity Framework内容。

Lazy loading is a complex process that requires your code to know about how the data comes from the database. As you don't want your domain classes to know about the database and the EF stuff, you abstract your entities from EF and add virtual properties, so EF can override your base POCO and add its DB-related stuff on runtime.

延迟加载是一个复杂的过程,需要您的代码知道数据是如何来自数据库的。由于您不希望域类了解数据库和EF内容,因此您可以从EF中抽象实体并添加虚拟属性,因此EF可以覆盖基础POCO并在运行时添加与DB相关的内容。

Same for change tracking.

更改跟踪也是如此。

#2


1  

Adding virtual allows EF to generate a derived class that overrides the property and returns a set from the database.

添加虚拟允许EF生成一个派生类,该类覆盖该属性并从数据库返回一个集合。