I\'m struggling with updating existing lecturer\'s data in database.
Every lecturer has Name, AcademicDegree and Course
Setting the state to Modified
and SetValues
only updates scalar properties of the Lecturer
entity. Updating the Courses
collection (which is not a scalar property) needs more work. You must handle the cases that a course could have been removed from the collection, a course could have been added or the scalar properties of a course could have been modified.
Also the way to update the collection depends on a course being dependent on the lecturer or not. Does the course need to be deleted from the database when it has been removed from the collection or does only the relationship between lecturer and course need to be removed? Does a new course need to be created when it has been added to the collection or does only a relationship need to be established?
If no courses must be deleted and no new courses be created the Update method could look like this:
public void UpdateLecturer(Lecturer lecturer)
{
using(var db = new AcademicTimetableDbContext())
{
if (lecturer == null)
return;
var lecturerInDb = db.Lecturers
.Include(l => l.Courses)
.Single(l => l.Id_Lecturer == lecturer.Id_Lecturer);
// Update lecturer
db.Entry(lecturerInDb).CurrentValues.SetValues(lecturer);
// Remove courses relationships
foreach (var courseInDb in lecturerInDb.Courses.ToList())
if (!lecturer.Courses.Any(c => c.Id_Course == courseInDb.Id_Course))
lecturerInDb.Courses.Remove(courseInDb);
foreach (var course in lecturer.Courses)
{
var courseInDb = lecturerInDb.Courses.SingleOrDefault(
c => c.Id_Course == course.Id_Course);
if (courseInDb != null)
// Update courses
db.Entry(courseInDb).CurrentValues.SetValues(course);
else
{
// Add courses relationships
db.Courses.Attach(course);
lecturerInDb.Courses.Add(course);
}
}
}
db.SaveChanges();
}
Depending on the details of your scenario the correct solution might be slightly different.
Edit
If the courses in the lecturer.Courses
collection have a reference to the lecturer
(having a Lecturer
navigation property maybe) you could have problems when you attach a course from this collection to the context because lecturerInDb
is already attached and has the same key. You can try to change the last else
block like so to solve the problem hopefully:
else
{
// Add courses relationships
var courseToAttach = new Course { Id_Course = course.Id_Course };
db.Courses.Attach(courseToAttach);
lecturerInDb.Courses.Add(courseToAttach);
}