【问题标题】:While saving some Client note, am getting this error " See 'EntityValidationErrors' property for more details"在保存一些客户注释时,我收到此错误“有关更多详细信息,请参阅 'EntityValidationErrors' 属性”
【发布时间】:2016-03-31 23:33:24
【问题描述】:

您好,我正在尝试保存客户说明,但它显示此错误,请有人帮我解决此问题。我在我的应用程序中使用注释页面,它将保存到某些单词。如果越过它并尝试保存,则会将错误显示为“一个或多个实体的验证失败。有关更多详细信息,请参阅 'EntityValidationErrors' 属性。”

【问题讨论】:

标签: c# asp.net entity-framework


【解决方案1】:

使用这段代码,它将准确地告诉你哪个实体验证失败的列,并且你会很容易到达它产生问题的地方。

 try
 {
     db.SaveChanges();
 }
 catch (DbEntityValidationException ex)
 {
     // Retrieve the error messages as a list of strings.
     var errorMessages = ex.EntityValidationErrors
                           .SelectMany(x => x.ValidationErrors)
                           .Select(x => x.ErrorMessage);

     // Join the list to a single string.
     var fullErrorMessage = string.Join("; ", errorMessages);

     // Combine the original exception message with the new one.
     var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

    // Throw a new DbEntityValidationException with the improved exception message.
   throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
 }

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2019-03-20
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多