在保存一些客户端备注的同时,我得到这个错误“有关详细信息,请参阅“EntityValidationErrors”属性“ [英] While saving some Client note, am getting this error " See 'EntityValidationErrors' property for more details"

查看:123
本文介绍了在保存一些客户端备注的同时,我得到这个错误“有关详细信息,请参阅“EntityValidationErrors”属性“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在保存客户端注意,但显示此错误,请帮助我解决这个问题。我在我的应用程序中使用笔记页面,它将保存到某些单词。如果正在跨越并尝试保存,则将错误显示为一个或多个实体的验证失败。有关详细信息,请参阅EntityValidationErrors属性。

解决方案

使用这段代码,这将直接告诉你哪个实体验证失败的列,你会很容易到达创建问题的地方。

  try 
{
db.SaveChanges();
}
catch(DbEntityValidationException ex)
{
//将错误消息作为字符串列表检索。
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);

//将列表加入单个字符串。
var fullErrorMessage = string.Join(;,errorMessages);

//将原始异常消息与新的异常消息相结合。
var exceptionMessage = string.Concat(ex.Message,验证错误是:,fullErrorMessage);

//使用改进的异常消息抛出一个新的DbEntityValidationException。
抛出新的DbEntityValidationException(exceptionMessage,ex.EntityValidationErrors);
}


Hi, am trying save the Client Note but it's showing this error, please can someone help me to resolve this. Am using a notes page in my application, it will save until certain words. If am crossing that and trying to save, it's showing the error as"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

解决方案

Use this chunk of code, this will tell you exactly on what column of which entity validation is failing, and you will eaisly reach the place where its creating problem.

 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);
 }

这篇关于在保存一些客户端备注的同时,我得到这个错误“有关详细信息,请参阅“EntityValidationErrors”属性“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆