public void should_Add_Student() { using (var dbContextTransaction = _dbContext.Database.BeginTransaction()) { var student = new Student() { RefNo = "12456343", FirstName = "John", LastName = "Smith", DateOfBirth = DateTime.Now.AddYears(-10), CampusID = 1 }; _studentService.Insert(student); Assert.True(student.StudentID > 0); dbContextTransaction.Rollback(); } }
public async Task <IActionResult> PostStudent(Student student) { try { if (ModelState.IsValid) { await StudentsService.Insert(student); return(CreatedAtAction(nameof(GetStudent), new { id = student.ID }, student)); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log) ModelState.AddModelError("", "Unable to saves changes. " + "Try again, and if the problems persists " + "see your system administrator"); } return(BadRequest()); }
//添加 public static int Add(Students students) { return(service.Insert(students)); }