gpt4 book ai didi

c# - POCO (EF) 类中 ICollection 的用途是什么?

转载 作者:行者123 更新时间:2023-12-02 22:12:56 25 4
gpt4 key购买 nike

public virtual ICollection<Product> Products { get; set; } 的目的是什么?在 POCO 类(class)中?

public class Category
{
[ScaffoldColumn(false)]
public int CategoryID { get; set; }

[Required, StringLength(100), Display(Name = "Name")]
public string CategoryName { get; set; }

public virtual ICollection<Product> Products { get; set; }


}

最佳答案

Category 相关的 Products 将在第一次访问该属性时由 Entity Framework 延迟加载。

如何加载相关实体在 Loading Related Entities 中有更详细的解释。 :

Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

因此Entity Framework将从Category类派生一个代理类,并重写Products属性的getter,以便在第一次访问该属性时加载相关产品。

关于c# - POCO (EF) 类中 ICollection 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14949400/

25 4 0