/// <summary>
 /// Checks if a given exception represents a Unique Constraint Violation with a given name
 /// </summary>
 /// <param name="exception">Exception instance</param>
 /// <param name="name">Constraint Name or part of it</param>
 /// <returns>True if unique constraint violation was detected. False otherwise</returns>
 public static bool IsUniqueConstraintViolation(this Exception exception, string name = null)
 {
     return(exception
            .AllSqlErrors()
            .Any(e =>
                 (e.Number == 2627 || e.Number == 2601) /* SELECT * FROM master.dbo.sysmessages */ &&
                 (string.IsNullOrWhiteSpace(name) || e.Message.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0)));
 }
 public static bool IsDeleteStatementConflictedWithReference(this Exception exception)
 {
     return(exception
            .AllSqlErrors()
            .Any(e =>
                 (e.Number == 547 && e.Message.IndexOf("delete", StringComparison.OrdinalIgnoreCase) >= 0 &&
                  e.Message.IndexOf("reference", StringComparison.OrdinalIgnoreCase) >= 0)));
 }