try { db.SaveChanges(); }
catch (DbEntityValidationException ex)
{
StringBuilder errors = new StringBuilder();
IEnumerable<DbEntityValidationResult> validationResult = ex.EntityValidationErrors;
foreach (DbEntityValidationResult result in validationResult)
{
ICollection<DbValidationError> validationError = result.ValidationErrors;
foreach (DbValidationError err in validationError)
{
errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n");
}
}
Console.WriteLine(errors.ToString());
}
原文地址:https://www.cnblogs.com/LiHeQ/p/8657553.html