Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a little question about entity framework entity models. The entity framework creates model's navigation properties with ICollection. But I need List's because of indexing.

My question is; Can I change these properties to Lists like below?

Thank you.

What I have tried:

C#
public partial class Student
{
    public Student()
    {
        Courses = new HashSet<Course>();
    }

    public int ID;
    public string Name;

    public virtual ICollection<Course> Courses { get; set; }
}


to

public partial class Student
{
    public Student()
    {
        Courses = new HashSet<Course>();
    }

    public int ID;
    public string Name;

    public virtual IList<Course> Courses { get; set; }
}
Posted
Updated 30-Jan-18 4:29am
Comments
F-ES Sitecore 30-Jan-18 10:16am    
Can't you just use ToList() when needed?

1 solution

Using IList<T> will work fine, although it doesn't really make sense to index into the list, since there won't be a pre-defined order for the related entities.

However, HashSet<T> doesn't implement IList<T>, so you'll have to pick a different class - List<T> perhaps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900