I'd like to catch SQL exception and differ between primary key violation and unique key violation. These two types of exceptinons returns the same ErrorCode 2627.
try
{
}
catch (SqlException ex)
{
if (ex.Number == 2627)
{
throw new UniqueOrDuplicateKeyException("Unique key or Primary key duplication")
}
}
It is nice but I'd like to throw UniqueKeyException or PrimaryKeyException. I know possibility to recognize which exception to throw, but it is parsing error message that is starting with 'Violation of UNIQUE KEY constraint' or 'Violation of PRIMART KEY constraint'. Of course, I'd like to avoid this option.
Another possibility is to do it directly in my stored procedure but I have a lot of stored procedures and it coul'd be quite annoying to add it everywhere.
Do you know some possiblities how to handle this issue in elegant way?