private Exception WrapSqlException(SqlException sex, EntityModel em) {//TODO: handle other errors as well: already has a relation, relations exists so cannot delete an item if (sex.Number == 2627) { var term = "constraint 'UK_"; var start = sex.Message.IndexOf(term) + term.Length; var end = sex.Message.IndexOf("'"[0], start); var constraintName = sex.Message.Substring(start, end - start); UniqueRuleModel rule = GetUniqueRuleFromConstraintName(constraintName, em); return(new UniqueRuleViolationException(rule)); } else if (sex.Number == 2601) { string term = "with unique index 'UK_"; var start = sex.Message.IndexOf(term) + term.Length; var end = sex.Message.IndexOf("'"[0], start); var cnstName = sex.Message.Substring(start, end - start); var rule = GetUniqueRuleFromConstraintName(cnstName, em); return(new UniqueRuleViolationException(rule)); } else if (sex.Number == 547) { throw new RelationExistsException(sex); } else { return(sex); } }
public UniqueRuleViolationException(UniqueRuleModel rule) : base(string.Format("Unique rule violation for properties: {0}", string.Join(", ", rule.Properties.Select(p => p.Name).ToArray()))) { Rule = rule; }