public override AZTException CreateAZTException(DbEntityValidationException ex) { int validationErrorCount = 0; string[] messages = ex.EntityValidationErrors .SelectMany( dbEntityValidationResult => dbEntityValidationResult.ValidationErrors.Select( dbValidationError => { ++validationErrorCount; DbEntityEntry entityEntry = dbEntityValidationResult.Entry; string entityTypeName = entityEntry.Entity.GetType().Name; string propertyName = dbValidationError.PropertyName; object originalValue = entityEntry.State != EntityState.Added && entityEntry.State != EntityState.Detached ? entityEntry.OriginalValues[propertyName] : null; object currentValue = entityEntry.State != EntityState.Deleted && entityEntry.State != EntityState.Detached ? entityEntry.CurrentValues[propertyName] : null; string errorMessage = dbValidationError.ErrorMessage; return (new StringBuilder() .AppendLine() .AppendLine($"{entityTypeName}.{propertyName}:") .AppendLine($" OriginalValue=[{originalValue ?? "(null)"}]") .AppendLine($" CurrentValue=[{currentValue ?? "(null)"}]") .AppendLine($" ErrorMessage=[{errorMessage}]") .ToString()); } ) ) .ToArray(); int entitiesAffected = ex.EntityValidationErrors.Count(); var newException = new AZTException( String.Format( "Validation failure: {0} {1}, {2} {3}.", entitiesAffected, entitiesAffected == 1 ? "entity" : "entities", validationErrorCount, validationErrorCount == 1 ? "error" : "errors" ), ExceptionMessageCollection.Create(messages) ); return(newException); }
public virtual async Task <int> SaveChangesAsync() { try { SetTimestamps(); await InternalSaveChangesAsync(); int affectedRows = await Context.SaveChangesAsync(); return(affectedRows); } catch (Exception ex) { if (ExceptionHandlersByType.TryGetValue(ex.GetType(), out ExceptionHandler handler)) { AZTException oloEx = handler.CreateAZTException(ex); throw oloEx; } throw; } }