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

前端之家收集整理的这篇文章主要介绍了为什么我需要在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类的实际实例类型具有非常奇怪的名称,因为它们是快速生成的.

原文链接:https://www.f2er.com/csharp/94098.html

猜你在找的C#相关文章