First table: School (Id, Name)
Second table: Student (Id, SchoolId, Name)
Suppose I have received updated roster for a school:
var school = db.Schools.Single(s => s.Id == updatedSchoolId);
school.Students = updatedStudents;
db.SubmitChanges();
This does not work, since disconnected students will end up with SchoolId
set to null (orphants).
Question: what is a preferred way to update child collection?
or am I stuck with executing DeleteOnSubmit
to get rid of orphans?
I would prefer a complete example.
At this time I compare both collections to produce 3 lists: removed items, added items, matched items. But I had to code this collection comparison, and it takes 12 lines of code, and it ain't pretty.