Using Entity Framework to query a MySQL database, one of my tables is giving the error Unable to cast object of type 'System.DBNull' to type 'System.String'.
I know if I can figure out which column is giving me the error I can fix it, but I can't seem to find any way to get EF to give me the column name. I've inspected all the columns in the code and find no issues; the data in the database doesn't have any gaps in the required fields; I figured out how to set the logging to Trace
in my appsettings.json, but aside from showing me the query it sends to the database, I don't see any change in the information that gets logged: it still just says Unable to cast object of 'System.DBNull' to type 'System.String'.
Is there any way to get EF to tell me what column it is having trouble with? The only next step I can think of is turning off Just My Code and hoping a deep dive into the source won't get too confusing.
CodePudding user response:
You should check out this place: https://docs.microsoft.com/en-us/ef/core/logging-events-diagnostics/
specifically i think You can find this bit useful: https://docs.microsoft.com/en-us/ef/core/logging-events-diagnostics/simple-logging#detailed-query-exceptions
try turning on detailed query exceptions logging for more details
CodePudding user response:
I was going to try T. Nielsen's suggestion after I finished fixing all the nullable issues that popped up when I enabled nullable reference type checking; but that accidentally led to an alternate way of finding which column wasn't set the way I thought it was: I set up a constructor on the class containing only the fields that were required, and the compiler told me Non-nullable property 'MyProblemColumn' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Which was exactly the reason I was trying to find it.