Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

详细信息中没显示是哪个字段验证出错:

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 + "\r\n");
        }
    }
    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'
上一篇:Eclipse启动报错An internal error occurred during: "Initializing Java Tooling"


下一篇:tortoiseGit 如何github提交代码