Example #1
0
        public static IStatusGeneric SaveChangesExceptionHandler(Exception e, DbContext context)
        {
            var dbUpdateEx = e as DbUpdateException;

            if (dbUpdateEx?.InnerException is SqlException sqlEx)
            {
                Console.WriteLine($"SQL Exception #{sqlEx.Number}: {sqlEx.Message}");
                //This is a DbUpdateException on a SQL database

                //if (sqlEx.Number == SqlServerViolationOfUniqueIndex ||
                //    sqlEx.Number == SqlServerViolationOfUniqueConstraint)
                //{
                //We have an error we can process
                var valError = UniqueErrorFormatter(sqlEx, dbUpdateEx.Entries);
                if (valError != null)
                {
                    var status = new StatusGenericHandler();
                    status.AddValidationResult(valError);
                    return(status);
                }
                //else check for other SQL errors
                //}
            }
            return(null);
        }