开发者

Elmah and DbEntityValidationException

I have setup a project with both Elmah and EF4.1 Code First.

The project is throwing a System.Data.Entity.Validation.DbEntityValidationException, but Elmah is not providing enough detail to determine what validation is failing. All that is logged is:

System.Data.Entity.Validation.DbEntityValidationExc开发者_Go百科eption: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Is there a way to make Elmah expand and log the 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜