gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:08:17 27 4
gpt4 key购买 nike

我有以下类(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 自动加载时

http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

例如,当使用下面定义的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com