• 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 + " ");
            }
        }
        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'
  • 相关阅读:
    python 基础 7.1 datetime 获得时间
    Python 学习笔记12
    Python 学习笔记11
    Python 学习笔记10
    Python 学习笔记9
    Python 学习笔记8
    Python 学习笔记7
    Python 学习笔记6
    Python 学习笔记5
    Python 学习笔记4
  • 原文地址:https://www.cnblogs.com/xdot/p/5254041.html
Copyright © 2020-2023  润新知