Entity Framework:Why the collection type of entity class need to be instanced in the default constructor?

那年仲夏 提交于 2019-12-07 02:20:16

问题


I am using Visual Studio to build a code first model of NorthWind database automatically. I have some questions.

  1. 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?

  2. The ICollection<T> is instantiated as a HashSet<T> in the default constructor. Why is HashSet<T> used? Can i use List<T> or something else?

  3. Why is the navigation property at one side (one to many relation) is ICollection<T> and virtual?

To Implement the entity class in the way I mention above, I think there must be some benefits can be brought. Can you tell me why?

public partial class Orders
{
    public Orders()
    {
        Order_Details = new HashSet<Order_Details>();
    }
    public virtual ICollection<Order_Details> Order_Details { get; set; }
}

回答1:


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?

You don't. It just needs to be instantiated before you start adding stuff to it.

The ICollection is instantiated as a HashSet in the default constructor. Why is HashSet used? Can i use List or something else?

You can use anything that implements ICollection<T> as the concrete implementation.

Why is the navigation property at "one" side (one to many relation) is ICollection and virtual?

ICollection<T> is the interface that EF expects for navigation properties. It provides the minimal interface necessary to represent that type of relationship. It is virtual so that EF can insert a proxy at runtime to detect changes to the property. If you decide not to make it virtual, you will need to manually inform EF about changes to the property.




回答2:


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?

For the same reason you instantiate any reference type field in your class. When you access it for the first time, it will be usable and not throw a 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> is used because it guarantees that two values which are equal to each other (which are equality checked by looking at their GetHashCode and Equals methods) only appear once in the collection. And yes, you can change the concrete type to any type which implements ICollection<T>.

Why is the navigation property at "one" side (one to many relation) is ICollection and virtual?

Because if a certain object has a one-to-many relationship, it means that each instance (one) will have a collection of a different type (many). It is virtual to allow EF to inject proxies at runtime.



来源:https://stackoverflow.com/questions/29873671/entity-frameworkwhy-the-collection-type-of-entity-class-need-to-be-instanced-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!