private static void TestValidationProperty() { using (var context = new InstituteEntities()) { //create entity var student = context.Students.Create(); student.StudentLastName = "Siddiqui"; student.GradePointAverage = 5; var department = context.Departments.Create(); department.DepartmentName = "Electrical Engineering"; //add it to context context.Students.Add(student); context.Departments.Add(department); //validate entity ICollection <DbValidationError> validationErrors = context.Entry <Student>(student) .Property <string>(s => s.StudentLastName) .GetValidationErrors(); string errorMessage = DbEntityValidationMessageParser.GetErrorMessage(validationErrors); //Print Message Console.WriteLine(errorMessage); } }
public IHttpActionResult PutStudent(int id, Student student) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != student.Id) { return(BadRequest()); } db.Entry(student).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
private static void TestValidationEntity() { using (var context = new InstituteEntities()) { //create entity var student = context.Students.Create(); student.GradePointAverage = 2; student.IsOutStanding = true; var department = context.Departments.Create(); department.DepartmentName = "Electrical Engineering"; //add it to context context.Students.Add(student); context.Departments.Add(department); //validate only student entity DbEntityValidationResult validationResult = context.Entry <Student>(student).GetValidationResult(); string errorMessage = DbEntityValidationMessageParser.GetErrorMessage(validationResult); //Print Message Console.WriteLine(errorMessage); } }