I am using Linq-to-SQL for accessing SQL Server 2008.
In database I have UNIQUE index on some column.
When I enter duplicated data in that column, LINQ will throw SqlException with an error message. Is there a way to figure out that exception is related to UNIQUE index and not some other SQL error?
Solution I am aware of is to user RegEx to parse exception message, but I would like to know is there more elegant approach?
You could use the
SQLException.Numberproperty.For more details you could iterate the
Errorscollection each of which have thier ownnumber.As an aside, in the past I have written my own custom serialiser for
SqlExceptionbut it appears that in 4.5ToString()has been overridden so this is not now necessary.