17

I'm trying to write a relational database application using Entity Framework 6. I have classes analogous to:

public class Subject
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

public class Student
{
    public int ID { get; set; }
    public int SubjectID { get; set; }
    public string Name { get; set; }
    public virtual Subject Subject { get; set; }
}

(OK this is a bad example because in reality you'd want each student to be in more than one subject but let's ignore this for now as it was the best example that I could think of.)

The problem is that, whenever there's a subject with no students, instead of subjectInstance.Students returning an empty collection it instead returns null. This means that I cannot call subjectInstance.Students.Add(studentInstance) to add the first student. I instead have to add the student separately, by calling contextInstance.Students.Add(studentInstance) after manually setting the SubjectID field on studentInstance. Once there's one or more students already associated with the subject, subjectInstance.Students is no longer null and I can add further students in the expected way.

What I've already tried:

  • Removing virtual from public virtual ICollection<Student> Students { get; set; } - no change

  • Calling contextInstance.Entry(subjectInstance).Collection("Students").Load() before attempting to access the collection - works but it's messy and breaks separation of concerns (the modules that work with the data shouldn't have to concern themselves with loading the data)

  • Calling contextInstance.Subjects.Include("Students") at some point before creating subjectInstance - no change

8
  • You can initialize the collection property (inside the constructor or with initializer) to new HashSet<Student> or new List<Student>.
    – Ivan Stoev
    Sep 10, 2017 at 13:32
  • 1
    You should always initialize collection navigation properties inside constructor. In your example you should do: Students = new HashSet<Student>(); inside your Subject constructor (and, of course, keep the property as virtual). Sep 10, 2017 at 13:33
  • @IvanStoev I thought that interferes with lazy loading? I thought that Entity Framework needed to use its own subclass/implementation of ICollection. Sep 10, 2017 at 13:46
  • @FedericoDipuma I thought that interferes with lazy loading? I thought that Entity Framework was supposed to create its own subclass/implementation of ICollection (as it does when the database is non-empty), and that it wouldn't function correctly if you tried to assign your own instance. I also haven't seen this mentioned in any tutorials or examples anywhere, the constructor is always left blank and it's up to Entity Framework to create and assign stuff. Sep 10, 2017 at 13:47
  • Actually, no. As stated in this thread, it you should initialize collections, but it is not strictly necessary. Sep 10, 2017 at 13:51

1 Answer 1

30

As the official documentation demonstrates, you should always initialize your collection navigation properties inside the entity constructor if you want to prevent a Null reference exception.

public Subject()
{
    Students = new HashSet<Student>(); // you may also use List<Student>, but HashSet will guarantee that you are not adding the same Student mistakenly twice
}

Entity framework will fill Students property (using a proxy) only if there is at least a student, else it will leave the property as is (null if you have not initialized it).

When the entity is not a proxy, then Entity Framework tracks its changes only when calling SaveChanges() on the context, using its original entity state for comparison. This answer will further clarify this behavior.

3
  • Thanks, very helpful explanation of how Entity Framework detects changes made using my own instance and the difference between List and HashSet as it relates to this situation. Sep 10, 2017 at 14:08
  • Is this valid for Entity Framework Core? Also, if you initialize a virtual collection from the constructor you are faced with this: stackoverflow.com/questions/119506/…
    – petko
    May 31, 2022 at 4:53
  • Took me a second to find where in the docs this was described, and it is literally a demonstration (as in you won't find "constructor" or "initialize" in a description on the page; the suggestion is limited to a line of sample code): this.Courses = new HashSet<Course>(); Not a critique of the answer here, instead a signpost for whomever looks for it next so you know it's not an explicitly described best practice [and is limited to an implied, but very smart (imo ymmv), one].
    – ruffin
    Jun 13, 2022 at 18:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.