我有以下类(class):
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; }
}
有人能解释一下为什么他们的客户信息是用虚拟编码的吗?什么意思?
最佳答案
看评论,你在学Entity Framework?
这里的虚拟意味着您正在尝试使用延迟加载 - 当相关项目(如 Customer)可以由 EF 自动加载时
例如,当使用下面定义的 Princess 实体类时,相关的 unicorn 将在第一次访问 Unicorns 导航属性时加载:
public class Princess
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Unicorn> Unicorns { get; set; }
}
关于c# - 为什么我需要在 C# 类中使用虚拟修饰符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750425/