Class Student
{
public string Name { get; set; }
public List<Location> Locations { get; set; }
}
The scenario goes like this:
- Page is loaded to client with Student using entity framework(EF) 6
- List of Locations edited by client by removing one, but another added
- Student is posted back to save and auto-bound to the model
I could attach Student back to the EF context using:
context.Entry(Student).Attach();
The problem is that EF will not detect the changes in Locations. I have to:
- Save Locations in a new Locations list
- Set Student.Locations = null;
- Then attach Student to the DbContext
- Loop through all Student.Locations and Remove each one
- Loop through all the new Locations list that I saved, pull the location from the database, then Add to Student
- Finally Save
What a pain. Any ideas?