Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlException constraint violation

I'm working on an asp.net app. Is there a way, when catching a SqlException, to know which constraint was violated?

like image 429
DJPB Avatar asked Dec 12 '22 23:12

DJPB


1 Answers

SqlException has a collection of SqlError objects: Errors. The SqlError have properties for error Number and you can compare this with the known constraint violation error numbers (eg. 2627).

While is true that SqlException itself exposes a Number property, it is not accurate if multiple errors happen in a single batch and hence is better to inspect the Errors collection.

like image 180
Remus Rusanu Avatar answered Dec 15 '22 11:12

Remus Rusanu