为什么要使用“虚拟”在实体框架模型定义类的属性? [英] Why use 'virtual' for class properties in Entity Framework model definitions?

查看:305
本文介绍了为什么要使用“虚拟”在实体框架模型定义类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下博客:<一href=\"http://weblogs.asp.net/scottgu/archive/2010/07/16/$c$c-first-development-with-entity-framework-4.aspx\">http://weblogs.asp.net/scottgu/archive/2010/07/16/$c$c-first-development-with-entity-framework-4.aspx

该博客包含以下code样品:<​​/ P>

The blog contains the following code sample:

public class Dinner
{
   public int DinnerID { get; set; }
   public string Title { get; set; }
   public DateTime EventDate { get; set; }
   public string Address { get; set; }
   public string HostedBy { get; set; }
   public virtual ICollection<RSVP> RSVPs { get; set; }
}

public class RSVP
{
   public int RsvpID { get; set; }
   public int DinnerID { get; set; }
   public string AttendeeEmail { get; set; }
   public virtual Dinner Dinner { get; set; }
}

什么是使用虚拟的目的定义一个类中的属性时?它有什么样的影响?

What is the purpose of using virtual when defining a property in a class? What effect does it have?

推荐答案

它允许实体框架创建一个围绕虚拟财产的代理,使该属性可以支持延迟加载,更高效的变更跟踪。见<一href=\"http://stackoverflow.com/questions/5597760/what-effects-can-the-virtual-keyword-have-in-entity-framework-4-1-poco-$c$c-fi\">What效果(S)可以在实体框架4.1 POCO code首先virtual关键字呢?,了解更深入的讨论。

It allows the Entity Framework to create a proxy around the virtual property so that the property can support lazy loading and more efficient change tracking. See What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First? for a more thorough discussion.

修改,以澄清围绕创建一个代理:
通过创建一个代理围绕我专门指的是实体框架做什么。实体框架要求您的导航属性被标记为虚,使延迟加载和高效的变更跟踪支持。请参阅创建POCO代理 要求。结果
实体框架使用继承来支持此功能,这就是为什么它需要特定的属性被标记在你的基类波苏斯虚。它的字面创造了新的类型,从你的POCO类型派生。所以,你的POCO是作为一个基本类型实体框架的动态创建的子类。这就是我的意思是围绕创建一个代理。

Edit to clarify "create a proxy around": By "create a proxy around" I'm referring specifically to what the Entity Framework does. The Entity Framework requires your navigation properties to be marked as virtual so that lazy loading and efficient change tracking are supported. See Requirements for Creating POCO Proxies.
The Entity Framework uses inheritance to support this functionality, which is why it requires certain properties to be marked virtual in your base class POCOs. It literally creates new types that derive from your POCO types. So your POCO is acting as a base type for the Entity Framework's dynamically created subclasses. That's what I meant by "create a proxy around".

动态创建的子类实体框架使用实体框架在运行的时候,而不是在静态编译时创建变得明显。只有当您启用了实体框架的延迟加载或更改跟踪功能。如果您选择从不使用延迟加载或改变实体框架的跟踪功能(这是不是默认的),那么你不必声明任何您的导航属性,虚拟的。然后,您对自己负责装载这些导航属性,无论是使用何种实体框架称之为预先加载,或手动检索多个数据库查询相关类型。你可以和应该使用延迟加载和改变,虽然在许多情况下为您导航性能跟踪功能。

The dynamically created subclasses that the Entity Framework creates become apparent when using the Entity Framework at runtime, not at static compilation time. And only if you enable the Entity Framework's lazy loading or change tracking features. If you opt to never use the lazy loading or change tracking features of the Entity Framework (which is not the default) then you needn't declare any of your navigation properties as virtual. You are then responsible for loading those navigation properties yourself, either using what the Entity Framework refers to as "eager loading", or manually retrieving related types across multiple database queries. You can and should use lazy loading and change tracking features for your navigation properties in many scenarios though.

如果您要创建一个独立的类和标记属性,虚拟的,并简单地构建,并在自己的应用程序使用这些类的实例,完全不在实体框架的范围,那么你的虚拟财产就不会得到你任何事情靠自己。

If you were to create a standalone class and mark properties as virtual, and simply construct and use instances of those classes in your own application, completely outside of the scope of the Entity Framework, then your virtual properties wouldn't gain you anything on their own.

修改来形容,为什么物业将被标记为虚拟

属性,如:

 public ICollection<RSVP> RSVPs { get; set; }

难道领域,因此不应当被认为。这些被称为getter和setter,并在编译时,它们被转换成方法。

Are not fields and should not be thought of as such. These are called getters and setters and at compilation time, they are converted into methods.

//Internally the code looks more like this:
public ICollection<RSVP> get_RSVPs()
{
    return _RSVPs;
}

public void set_RSVPs(RSVP value)
{
    _RSVPs = value;
}

private RSVP _RSVPs;

这就是为什么他们标记为虚拟的实体框架中使用,它允许动态创建的类重写内部产生 GET 设置功能。如果您的导航属性的getter / setter方法​​是在你的实体框架的使用情况为你工作,尝试修改他们只是性能,重新编译,看看实体框架能仍然正常工作:

That's why they're marked as virtual for use in the Entity Framework, it allows the dynamically created classes to override the internally generated get and set functions. If your navigation property getter/setters are working for you in your Entity Framework usage, try revising them to just properties, recompile, and see if the Entity Framework is able to still function properly:

 public virtual ICollection<RSVP> RSVPs;

这篇关于为什么要使用“虚拟”在实体框架模型定义类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆