private static IStatusGeneric ExecuteValidation(this DbContext context)
        {
            var status = new StatusGenericHandler();

            foreach (var entry in
                     context.ChangeTracker.Entries()
                     .Where(e =>
                            (e.State == EntityState.Added) ||
                            (e.State == EntityState.Modified)))
            {
                var entity       = entry.Entity;
                var valProvider  = new ValidationDbContextServiceProvider(context);
                var valContext   = new ValidationContext(entity, valProvider, null);
                var entityErrors = new List <ValidationResult>();
                if (!Validator.TryValidateObject(
                        entity, valContext, entityErrors, true))
                {
                    status.AddValidationResults(entityErrors);
                }
            }

            return(status);
        }
Ejemplo n.º 2
0
        private static ImmutableList <ValidationResult> ExecuteValidation(this DbContext context)
        {
            var result = new List <ValidationResult>();

            foreach (var entry in
                     context.ChangeTracker.Entries()
                     .Where(e =>
                            (e.State == EntityState.Added) ||
                            (e.State == EntityState.Modified)))
            {
                var entity       = entry.Entity;
                var valProvider  = new ValidationDbContextServiceProvider(context);
                var valContext   = new ValidationContext(entity, valProvider, null);
                var entityErrors = new List <ValidationResult>();
                if (!Validator.TryValidateObject(
                        entity, valContext, entityErrors, true))
                {
                    result.AddRange(entityErrors);
                }
            }

            return(result.ToImmutableList());
        }