实体Framework:Why 实体class 的集合类型需要在默认构造函数中实例化吗?
Entity Framework:Why the collection type of entity class need to be instanced in the default constructor?
我正在使用 Visual Studio 自动构建 NorthWind 数据库的代码优先模型。我有一些问题。
我发现如果实体 class 有一个集合,那么这个集合总是在默认构造函数中实例化。为什么我们需要这样做?
ICollection<T>
在默认构造函数中被实例化为 HashSet<T>
。为什么使用 HashSet<T>
?我可以使用 List<T>
或其他东西吗?
为什么导航属性在一侧一侧(一对多关系)是ICollection<T>
和virtual
?
按照我上面说的方式来实现实体class,我想肯定是可以带来一些好处的。你能告诉我为什么吗?
public partial class Orders
{
public Orders()
{
Order_Details = new HashSet<Order_Details>();
}
public virtual ICollection<Order_Details> Order_Details { get; set; }
}
I found that if the entity class has a collection, then the is collection always instantiated in the default constructor. Why we need to do that?
你不知道。它只需要在你开始向它添加东西之前实例化。
The ICollection is instantiated as a HashSet in the default constructor. Why is HashSet used? Can i use List or something else?
您可以使用实现 ICollection<T>
的任何东西作为具体实现。
Why is the navigation property at "one" side (one to many relation) is ICollection and virtual?
ICollection<T>
是 EF 期望的导航属性接口。它提供了表示该类型关系所需的最小接口。它是虚拟的,因此 EF 可以在运行时插入代理以检测对 属性 的更改。如果您决定不将其设为虚拟,则需要手动通知 EF 有关 属性.
的更改
I found that if the entity class has a collection, then the is collection always instantiated in the default constructor. Why we need to do that?
出于同样的原因,您在 class 中实例化了任何引用类型字段。当您第一次访问它时,它将可用并且不会抛出 NRE。
The ICollection is instantiated as a HashSet in the default constructor. Why is HashSet used? Can i use List or something else?
使用 A HashSet<T>
是因为它保证两个彼此相等的值(通过查看它们的 GetHashCode
和 Equals
方法检查是否相等)只出现一次在集合中。
是的,您可以将具体类型更改为实现 ICollection<T>
.
的任何类型
Why is the navigation property at "one" side (one to many relation) is ICollection and virtual?
因为如果某个对象是一对多的关系,就意味着每个实例(一个)都会有一个不同类型(多个)的集合。允许 EF 在运行时注入代理是虚拟的。
我正在使用 Visual Studio 自动构建 NorthWind 数据库的代码优先模型。我有一些问题。
我发现如果实体 class 有一个集合,那么这个集合总是在默认构造函数中实例化。为什么我们需要这样做?
ICollection<T>
在默认构造函数中被实例化为HashSet<T>
。为什么使用HashSet<T>
?我可以使用List<T>
或其他东西吗?为什么导航属性在一侧一侧(一对多关系)是
ICollection<T>
和virtual
?
按照我上面说的方式来实现实体class,我想肯定是可以带来一些好处的。你能告诉我为什么吗?
public partial class Orders
{
public Orders()
{
Order_Details = new HashSet<Order_Details>();
}
public virtual ICollection<Order_Details> Order_Details { get; set; }
}
I found that if the entity class has a collection, then the is collection always instantiated in the default constructor. Why we need to do that?
你不知道。它只需要在你开始向它添加东西之前实例化。
The ICollection is instantiated as a HashSet in the default constructor. Why is HashSet used? Can i use List or something else?
您可以使用实现 ICollection<T>
的任何东西作为具体实现。
Why is the navigation property at "one" side (one to many relation) is ICollection and virtual?
ICollection<T>
是 EF 期望的导航属性接口。它提供了表示该类型关系所需的最小接口。它是虚拟的,因此 EF 可以在运行时插入代理以检测对 属性 的更改。如果您决定不将其设为虚拟,则需要手动通知 EF 有关 属性.
I found that if the entity class has a collection, then the is collection always instantiated in the default constructor. Why we need to do that?
出于同样的原因,您在 class 中实例化了任何引用类型字段。当您第一次访问它时,它将可用并且不会抛出 NRE。
使用The ICollection is instantiated as a HashSet in the default constructor. Why is HashSet used? Can i use List or something else?
A HashSet<T>
是因为它保证两个彼此相等的值(通过查看它们的 GetHashCode
和 Equals
方法检查是否相等)只出现一次在集合中。
是的,您可以将具体类型更改为实现 ICollection<T>
.
Why is the navigation property at "one" side (one to many relation) is ICollection and virtual?
因为如果某个对象是一对多的关系,就意味着每个实例(一个)都会有一个不同类型(多个)的集合。允许 EF 在运行时注入代理是虚拟的。