Beispiel #1
0
 public override async Task <int> SaveChangesAsync()
 {
     try
     {
         return(await base.SaveChangesAsync());
     }
     catch (System.Data.Entity.Validation.DbEntityValidationException e)
     {
         A.Core.Validation.ValidationResult validationResult = new A.Core.Validation.ValidationResult();
         foreach (var dbEntityValidationResult in e.EntityValidationErrors)
         {
             if (!dbEntityValidationResult.IsValid)
             {
                 foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors)
                 {
                     validationResult.ResultList.Add(new ValidationResultItem
                     {
                         Description = dbValidationError.ErrorMessage
                         ,
                         Level = ValidationResultLevelEnum.Error
                         ,
                         Key = dbValidationError.PropertyName
                     });
                 }
             }
         }
         throw new ValidationException(validationResult);
     }
 }
        public virtual async Task <A.Core.Validation.ValidationResult> ValidateAsync(object entity)
        {
            A.Core.Validation.ValidationResult result = new A.Core.Validation.ValidationResult();

            var context           = new ValidationContext(entity, serviceProvider: null, items: null);
            var validationResults = new List <System.ComponentModel.DataAnnotations.ValidationResult>();

            bool isValid = Validator.TryValidateObject(entity, context, validationResults, true);

            if (!isValid)
            {
                validationResults.ForEach(x => { result.ResultList.Add(new A.Core.Validation.ValidationResultItem()
                    {
                        Key = x.MemberNames.FirstOrDefault(), Description = x.ErrorMessage
                    }); });
            }
            return(await Task.FromResult(result));
        }
Beispiel #3
0
 public ValidationException(ValidationResult result) :
     base("Validation exception")
 {
     ValidationResult = result;
 }