This article comes from needing this capability in my Generic Services and Generic Biz Runner libraries while working on a project for a client.
I also extended the design to handle concurrency issues in both libraries, as one of the users of my Generic Services library was asking for it.
Compiled); public static Validation Result Unique Error Formatter(Sql Exception ex, IRead Only List Notice on lines 13 to 15 I try to find the entity class so that I can return a user friendly and localized name, but the name in the constraint/key name is pretty good (unless you have a TPH class).
The code above is called within my Save Changed With Validation method, which I show below.
It starts with the method that detects each type of error you want to handle, and then calling the appropriate method.
This is why my Generic Biz Runner library defaults to validating the data on save, while my Generic Services library, which works with the front-end CRUD (Create/Read/Update/Delete) accesses, defaults to not validating the data.
In the case of the Db Update Exception has an inner exception, which is the actual database exception containing the information about the constraint violation.
The type and content of this inner exception differs for each database type.
The method returns the IStatus Generic interface I use in my libiraries.
This contains either a success message if there were no errors, or a list of errors if problems were found.
Search for validating handles:
When EF Core writes data out to the database it doesn’t validate that data (see Introduction to validation section for more on validation).