为什么我需要在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类的实际实例类型具有非常奇怪的名称,因为它们是快速生成的.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...