public static Int32 SaveChangesWithTriggers(this DbContext dbContext, Func <Int32> baseSaveChanges, IServiceProvider serviceProvider) { #endif if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } var invoker = TriggerInvokers.Get(dbContext.GetType()); var swallow = false; try { var afterActions = invoker.RaiseTheBeforeEvents(dbContext, serviceProvider); #if EF_CORE var result = baseSaveChanges(acceptAllChangesOnSuccess); #else var result = baseSaveChanges(); #endif invoker.RaiseTheAfterEvents(dbContext, serviceProvider, afterActions); return(result); } catch (DbUpdateException ex) when(invoker.RaiseTheFailedEvents(dbContext, serviceProvider, ex, ref swallow)) { } #if !EF_CORE catch (DbEntityValidationException ex) when(invoker.RaiseTheFailedEvents(dbContext, serviceProvider, ex, ref swallow)) { } #endif catch (Exception ex) when(invoker.RaiseTheFailedEvents(dbContext, serviceProvider, ex, ref swallow)) { } return(0); }
public static async Task <Int32> SaveChangesWithTriggersAsync(this DbContext dbContext, Func <CancellationToken, Task <Int32> > baseSaveChangesAsync, IServiceProvider serviceProvider, CancellationToken cancellationToken = default) { #endif if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } var invoker = TriggerInvokers.Get(dbContext.GetType()); var swallow = false; try { var afterActions = invoker.RaiseTheBeforeEvents(dbContext, serviceProvider); #if EF_CORE var result = await baseSaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false); #else var result = await baseSaveChangesAsync(cancellationToken).ConfigureAwait(false); #endif invoker.RaiseTheAfterEvents(dbContext, serviceProvider, afterActions); return(result); } catch (DbUpdateException ex) when(invoker.RaiseTheFailedEvents(dbContext, serviceProvider, ex, ref swallow)) { } #if !EF_CORE catch (DbEntityValidationException ex) when(invoker.RaiseTheFailedEvents(dbContext, serviceProvider, ex, ref swallow)) { } #endif catch (Exception ex) when(invoker.RaiseTheFailedEvents(dbContext, serviceProvider, ex, ref swallow)) { } return(0); }