Why does EF error interface is so unhelpful ? How can one find the exact cause for a DbContext.SaveChanges exception, when the errors tell almost nothing about the parameters involved in the exception?
One serious shortcoming of EF
in my opinion is detecting what is causing a given exception after issuing SaveChanges()
.
It has been long since I have been struggling with EF error interface. I once helped myself a bit by writing a method that flattens all the db context validation errors providing straightforward messages with Table, Column and error message for each error. Otherwise one has to dig deep in the error structures dbContext returns... I wonder why they did this this way!
In any case, to my problem:
I perform several add operations in several entities in my dbContext, and in the end issue one single SaveChanges()
.
Now, I GetValidationErrors()
returns 0 errors, so no validation issues.
But SaveChanges
throws the following exception:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
How does DbContext
points out the specific cause of the error in any way when what he passes over to you is a nested labyrinth of inner objects that looks like:
?!