Beispiel #1
0
        /// <summary>
        /// This will validate any entity classes that will be added or updated
        /// If the validation does not produce any errors then SaveChangesAsync will be called
        /// </summary>
        /// <param name="context"></param>
        /// <param name="config"></param>
        /// <returns>List of errors, empty if there were no errors</returns>
        public static async Task <IStatusGeneric> SaveChangesWithValidationAsync(this DbContext context, IGenericServicesConfig config = null)
        {
            var status = context.ExecuteValidation();

            if (!status.IsValid)
            {
                return(status);
            }

            context.ChangeTracker.AutoDetectChangesEnabled = false;
            try
            {
                await context.SaveChangesAsync().ConfigureAwait(false);
            }
            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);
        }
Beispiel #2
0
        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);
        }