Hitscotty
2/15/2018 - 1:34 PM

DB Exception

Helsp log the reason why a db exception was thrown, by logging the required fields of table

catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                   {
                       Exception raise = dbEx;
                       foreach (var validationErrors in dbEx.EntityValidationErrors)
                       {
                           foreach (var validationError in validationErrors.ValidationErrors)
                           {
                               string message = string.Format("{0}:{1}",
                                   validationErrors.Entry.Entity.ToString(),
                                   validationError.ErrorMessage);
                               // raise a new exception nesting
                               // the current instance as InnerException
                               raise = new InvalidOperationException(message, raise);
                           }
                       }
                       throw raise;
                   }