Search This Blog

Showing posts with label Extension Methods. Show all posts
Showing posts with label Extension Methods. Show all posts

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