private static async Task <IStatusGeneric> SaveChangesWithExtrasAsync(this DbContext context, IGenericServicesConfig config, bool turnOffChangeTracker = false) { var status = config?.BeforeSaveChanges != null ? config.BeforeSaveChanges(context) : new StatusGenericHandler(); if (!status.IsValid) { return(status); } if (turnOffChangeTracker) { context.ChangeTracker.AutoDetectChangesEnabled = false; } try { //This handles SaveChangesExceptionHandlers that can fix the exception, and the SaveChanges i tried again do { try { await context.SaveChangesAsync().ConfigureAwait(false); break; //This breaks out of the do/while } catch (Exception e) { var exStatus = config?.SaveChangesExceptionHandler(e, context); if (exStatus == null) { throw; //error wasn't handled, so rethrow } status.CombineStatuses(exStatus); } } while (status.IsValid); } finally { context.ChangeTracker.AutoDetectChangesEnabled = true; } return(status); }
//----------------------------------------------------------------- //private methods private static IStatusGeneric SaveChangesWithExtras(this DbContext context, IGenericServicesConfig config, bool turnOffChangeTracker = false) { var status = config?.BeforeSaveChanges != null ? config.BeforeSaveChanges(context) : new StatusGenericHandler(); if (!status.IsValid) { return(status); } if (turnOffChangeTracker) { context.ChangeTracker.AutoDetectChangesEnabled = false; } try { context.SaveChanges(); } catch (Exception e) { var exStatus = config?.SaveChangesExceptionHandler(e, context); if (exStatus == null) { throw; //error wasn't handled, so rethrow } status.CombineStatuses(exStatus); } finally { context.ChangeTracker.AutoDetectChangesEnabled = true; } return(status); }