Sunday, August 30, 2015

Extension Method to Flatten Entity Framework Validations Errors - providing Entity, Property and Error message

    public static class ExtensionMethods
    {
        public static List FlattenErrors(this DbContext dbContext)
        {
            return dbContext
                .GetValidationErrors()
                .SelectMany(e =>
                    e.ValidationErrors
                    .Select(ve =>
                        string.Format("Entity: [{0}], Property: [{1}], Error: [{2}].", ObjectContext.GetObjectType(e.Entry.Entity.GetType()).Name, ve.PropertyName, ve.ErrorMessage)
                    ))
                .ToList();
        }
    }

No comments:

Post a Comment