The error message : System.InvalidOperationException: The property setter does not exist.
The actual problem: Entity Framework is not loading related items from a Many-To-Many mapping.
The solution: Make sure the parameterless constructor for both parts initialises the collection of the other one.
In detail:
I have two tables. Let us call them Students and Classes. They have a many-to-many relationship.
Student has virtual ICollection<Class> Classes. Class has virtual ICollection<Student> Students. This should enable lazy loading.
I am trying to deal with Class. In fact, I am trying to deal with ThisYearsClass - which has a Class (subject, prerequisites, etc) and also some additional info of its own.
As far as I can make out, Entity Framework hasn't loaded Students for the course. So when I say "give me ThisYearsClass.Class.Students", I get null.
When I try to copy ThisYearsClass.Class to a new Class, Students are an expected property, and not there. And at that point, I get a missing setter exception!
Go back to Class. In the parameterless constructor, put:
protected Class(){
Students = new List<Student>();
}
and similarly for Student, going the other way.
Problem solved!