I am using Dynamic Data and to apply Unique key and other database level validations i have overridden the SubmitChanges function.
This gives me original error, which i am able to show to the end user. But, this message is like this:
Violation of UNIQUE KEY constraint 'UK_CountryName_Country'. Cannot insert duplicate key in object 'dbo.Country'. The duplicate key value is (America).
The statement has been terminated.
I need the following keywords of the message:
- UNIQUE KEY
- UK_CountryName_Country
- America
I have written following Regex:
System.Text.RegularExpressions.Regex.Split(e.Message.ToString(), "^Violation of (UNIQUE KEY){1} constraint '([a-zA-Z_]*)'. Cannot insert duplicate key in object 'dbo.([a-zA-Z]*)'. The duplicate key value is ")
I have succeeded in getting #1 & #2, but #3.
Can anyone help me. Also, is there any cleaner way to achieve it.
FYI: I would be catching other types of database errors as well.
Thanks
With your example expression, it seemed you were trying to match the
Countryfromdbo.Countryinstead ofAmericaas you asked, so I included both in my expression/code:The expression is designed to be very tolerant of the type of error message, as there are other types of Key Constraints that can be violated. I could not find a list of the possible errors, but here is a breakdown of the expression:
You can get rid of the parts that look like
?<Something>if you do not want to use named capturing groups, and only want to use the numbers