public bool UpdateStudent(AddEditStudentDto stdPost) { try { //update first Student Student s = new Student(); s.Id = stdPost.Id; s.Name = stdPost.Name; s.FName = stdPost.FName; s.Dob = stdPost.Dob; s.Email = stdPost.Email; s.Phone = stdPost.Phone; s.UserId = stdPost.UserId; s.Password = stdPost.Password; s.ImageUrl = stdPost.ImageUrl; s.ThumbUrl = stdPost.ThumbUrl; s.ConfirmPassword = stdPost.ConfirmPassword; //now First Find Courses And delete previous courses for Student var stdPreCourses = context.StudentCourses.Where(x => x.StudentId == stdPost.Id).ToList(); if (stdPreCourses != null) { context.StudentCourses.RemoveRange(stdPreCourses); context.SaveChanges(); } //Now Add Newly Added Courses List <StudentCourse> courseList = new List <StudentCourse>(); if (stdPost.Courses != null) { foreach (var Course in stdPost.Courses) { StudentCourse course_Obj = new StudentCourse(); course_Obj.StudentId = stdPost.Id; course_Obj.CourseId = Convert.ToInt32(Course); courseList.Add(course_Obj); } context.StudentCourses.AddRange(courseList); context.Entry(s).State = EntityState.Modified; context.SaveChanges(); } //Adding updated Courses } catch (Exception e) { return(false); } return(true); }
public void Update(T obj) { _context.Entry(obj).State = EntityState.Modified; Save(); }