微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我需要在c#类中使用虚拟修饰符?

我有以下类:
public class Delivery
{
// Primary key,and one-to-many relation with Customer
   public int DeliveryID { get; set; }
   public virtual int CustomerID { get; set; }
   public virtual Customer Customer { get; set; }

// Properties
   string Description { get; set; }
}

有人可以解释为什么客户信息用虚拟编码.这是什么意思?

解决方法

猜猜你在使用EF.

当您制作NavigationProperty虚拟机时,会发生什么是EF动态创建派生类.
该类实现了允许延迟加载和其他任务的功能,例如维护EF为您执行的关系.

只是为了让你的抽象类动态变成这样的想法:

public class DynamicEFDelivery : Delivery 
{
   public override Customer Customer 
   { 
     get
     {
       return // go to the DB and actually get the customer
     } 
     set
     {
       // attach the given customer to the current entity within the current context
       // afterwards set the Property value
     }
   }
}

您可以在调试时轻松看到这一点,您的EF类的实际实例类型具有非常奇怪的名称,因为它们是快速生成的.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐