public override async Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { try { var auditLog = AuditingManager?.Current?.Log; List <EntityChangeInfo> entityChangeList = null; if (auditLog != null) { entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList()); } var changeReport = ApplyRocketConcepts(); var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); await EntityChangeEventHelper.TriggerEventsAsync(changeReport); if (auditLog != null) { EntityHistoryHelper.UpdateChangeList(entityChangeList); auditLog.EntityChanges.AddRange(entityChangeList); Logger.LogDebug($"Added {entityChangeList.Count} entity changes to the current audit log"); } return(result); } catch (DbUpdateConcurrencyException ex) { throw new RocketDbConcurrencyException(ex.Message, ex); } finally { ChangeTracker.AutoDetectChangesEnabled = true; } }
public override int SaveChanges(bool acceptAllChangesOnSuccess) { //TODO: Reduce duplications with SaveChangesAsync //TODO: Instead of adding entity changes to audit log, write them to uow and add to audit log only if uow succeed ChangeTracker.DetectChanges(); try { ChangeTracker.AutoDetectChangesEnabled = false; //TODO: Why this is needed? var auditLog = AuditingManager?.Current?.Log; List <EntityChangeInfo> entityChangeList = null; if (auditLog != null) { entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList()); } var changeReport = ApplyAbpConcepts(); var result = base.SaveChanges(acceptAllChangesOnSuccess); AsyncHelper.RunSync(() => EntityChangeEventHelper.TriggerEventsAsync(changeReport)); if (auditLog != null) { EntityHistoryHelper.UpdateChangeList(entityChangeList); auditLog.EntityChanges.AddRange(entityChangeList); } return(result); } catch (DbUpdateConcurrencyException ex) { throw new AbpDbConcurrencyException(ex.Message, ex); } finally { ChangeTracker.AutoDetectChangesEnabled = true; } }