public bool UpdateStudentProfile(Student student) { bool isSuccess = false; try { Student objStudent = GetStudentDetails(student.StudentId); Detach(objStudent); //StudentApiContext.Entry(objStu5dent).State = EntityState.Detached; if (student != null) { //objStudent = student; student.ModifiedOn = DateTime.Now; student.CreatedOn = objStudent.CreatedOn; student.BirthDate = student.BirthDate.AddDays(1); //StudentApiContext.Entry(student).State = EntityState.Modified; Update(student); StudentApiContext.SaveChanges(); isSuccess = true; } } catch (Exception ex) { throw ex; } return(isSuccess); }
// Save student public Student SaveStudentProfile(StudentViewModel studentViewModel) { Student student = new Student(); try { if (studentViewModel != null) { DateTime dtCurrentDate = DateTime.Now; student.FirstName = studentViewModel.FirstName; student.LastName = studentViewModel.LastName; student.GradeId = studentViewModel.GradeId; student.BirthDate = studentViewModel.BirthDate.AddDays(1); student.CreatedOn = dtCurrentDate; student.ModifiedOn = dtCurrentDate; StudentApiContext.Students.Add(student); StudentApiContext.SaveChanges(); } } catch (Exception ex) { throw ex; } return(student); }
// Delete student public bool DeleteStudent(int studentId) { bool isSuccess = false; try { Student student = StudentApiContext.Students.Find(studentId); if (student != null) { StudentApiContext.Students.Remove(student); StudentApiContext.SaveChanges(); isSuccess = true; } } catch (Exception ex) { throw ex; } return(isSuccess); }