Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; }
}
like image 310
LoremIpsum Avatar asked Apr 26 '15 04:04

LoremIpsum


2 Answers

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.

like image 65
Eric J. Avatar answered Oct 29 '22 13:10

Eric J.


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.

like image 45
Yuval Itzchakov Avatar answered Oct 29 '22 14:10

Yuval Itzchakov