public void AddCourseToGroup(int courseId, int groupId) { Course course = context.Courses .FirstOrDefault(c => c.CourseId == courseId); if (course != null) { Group group = context.Groups .FirstOrDefault(g => g.GroupId == groupId); if (group != null) { GroupCourses dbEntry = new GroupCourses { GroupId = group.GroupId, CourseId = course.CourseId }; dbEntry.CreatedAt = DateTime.Now; dbEntry.UpdatedAt = DateTime.Now; context.GroupCourses.Add(dbEntry); context.SaveChanges(); group.UpdatedAt = DateTime.Now; context.SaveChanges(); } } }
public void RemoveCourseFromGroup(int groupId, int coursId) { GroupCourses dbEntry = context.GroupCourses .FirstOrDefault(g => g.GroupId == groupId && g.CourseId == coursId); if (dbEntry != null) { context.GroupCourses.Remove(dbEntry); context.SaveChanges(); } Group group = context.Groups.FirstOrDefault(g => g.GroupId == groupId); group.UpdatedAt = DateTime.Now; context.SaveChanges(); }