I see this a lot in tutorials, with navigation properties as ICollection<T>
.
Is this a mandatory requirement for Entity Framework? Can I use IEnumerable
?
What's the main purpose of using ICollection
instead of IEnumerable
or even List<T>
?
I see this a lot in tutorials, with navigation properties as ICollection<T>
.
Is this a mandatory requirement for Entity Framework? Can I use IEnumerable
?
What's the main purpose of using ICollection
instead of IEnumerable
or even List<T>
?
I remember it this way:
IEnumerable has one method GetEnumerator() which allows one to read through the values in a collection but not write to it. Most of the complexity of using the enumerator is taken care of for us by the for each statement in C#. IEnumerable has one property: Current, which returns the current element.
ICollection implements IEnumerable and adds few additional properties the most use of which is Count. The generic version of ICollection implements the Add() and Remove() methods.
IList implements both IEnumerable and ICollection, and add the integer indexing access to items (which is not usually required, as ordering is done in database).
Navigation properties are typically defined as virtual so that they can take advantage of certain Entity Framework functionality such as lazy loading.
If a navigation property can hold multiple entities (as in many-to-many or one-to-many relationships), its type must be a list in which entries can be added, deleted, and updated, such as ICollection.
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application