gpt4 book ai didi

entity-framework-4.1 - Elmah 和 DbEntityValidationException

转载 作者:行者123 更新时间:2023-12-01 04:11:13 24 4
gpt4 key购买 nike

我已经使用 Elmah 和 EF4.1 Code First 设置了一个项目。

项目抛出System.Data.Entity.Validation.DbEntityValidationException ,但 Elmah 没有提供足够的细节来确定哪些验证失败。所有记录的是:

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

有没有办法让 Elmah 扩展并记录 EntityValidationErrors属性(property)?

最佳答案

List<IUserFeedback> errors = new List<IUserFeedback>();

try
{
_dbContext.SaveChanges();
Updated(this, HasUnsavedChanges);
}
catch (DbEntityValidationException ex)
{
foreach (var x in ex.EntityValidationErrors)
{
foreach (var y in x.ValidationErrors)
{
if (!String.IsNullOrWhiteSpace(y.PropertyName))
errors.Add(new UserFeedback() {
FeedbackFlags = TypeOfUserFeedbackFlags.Error,
Message = String.Format("Unable to save {0} due to an issue with its \"{1}\" value. The error returned was \"{2}\"",x.Entry.Entity, y.PropertyName, y.ErrorMessage)
});
else
errors.Add(new UserFeedback() {
FeedbackFlags = TypeOfUserFeedbackFlags.Error,
Message = String.Format("Unable to save {0} due to the error \"{1}\"", x.Entry, y.ErrorMessage)
});
}
}
}

return errors;

关于entity-framework-4.1 - Elmah 和 DbEntityValidationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6181487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com