Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
详细信息中没显示是哪个字段验证出错:
Validation failed for one or more entities解决方法
复制内容到剪贴板程序代码
try
{
Context.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 + " ");
}
}
Console.WriteLine(errors.ToString());
//简写
//var validerr = ex.EntityValidationErrors.First().ValidationErrors.First();
//Console.WriteLine(validerr.PropertyName + ":" + validerr.ErrorMessage);
}
{
Context.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 + " ");
}
}
Console.WriteLine(errors.ToString());
//简写
//var validerr = ex.EntityValidationErrors.First().ValidationErrors.First();
//Console.WriteLine(validerr.PropertyName + ":" + validerr.ErrorMessage);
}
结果显示:
引用内容
Imsi:The field Imsi must be a string or array type with a maximum length of '20'