// 2627 is unique constraint (includes primary key), 2601 is unique index catch (UpdateException ex) when (ex.InnerException is SqlException sqlException && (sqlException.Number == 2627 || sqlException.Number == 2601)) { }