I have simple repository method in my asp.net core webapi. I am using EF core 2.0. When i update the parent startdate property, i would like to update all the related children to have the same start date. How to achieve this? There is One to Many relation with Course and CourseFormat.
Here is my code snippet:
public Course SaveCourse(Course item)
{
if (item.CourseId == 0)
_context.Course.Add(item);
else
{
var existing = _context.Course.SingleOrDefault(course => course.CourseId == item.CourseId);
existing.CourseCode = item.CourseCode;
existing.CourseName = item.CourseName;
existing.Purpose = item.Purpose;
existing.CourseDescription = item.CourseDescription;
existing.AutomaticNotifications = item.AutomaticNotifications;
existing.StartDate = item.StartDate;
existing.EndDate = item.EndDate;
existing.ModifiedBy = item.ModifiedBy;
existing.Modified = item.Modified;
**// trying the following. is this right?**
foreach(var courseFormat in existing.CourseFormat)
courseFormat.StartYear = existing.StartDate.Value.Year;
item.CreatedBy = existing.CreatedBy;
item.Created = existing.Created;
}
_context.SaveChanges();
return item;
}