Pomelo.EntityFrameworkCore.MySql: Unable to cast object of type 'System.DBNull' to type 'System.String'

return (_context.Set<TEntity>().AsEnumerable())

I upgraded from dotnet 5 to 6 and now it started to get the error:

An exception occurred while iterating over the results of a query for context type 'KLog.Data.Context.KLogDbContext'.
System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.
   at MySqlConnector.Core.Row.GetString(Int32 ordinal) in /_/src/MySqlConnector/Core/Row.cs:line 380
   at MySqlConnector.MySqlDataReader.GetString(Int32 ordinal) in /_/src/MySqlConnector/MySqlDataReader.cs:line 283
   at lambda_method2(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
Exception thrown: 'System.InvalidCastException' in Microsoft.EntityFrameworkCore.Relational.dll
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.
   at MySqlConnector.Core.Row.GetString(Int32 ordinal) in /_/src/MySqlConnector/Core/Row.cs:line 380
   at MySqlConnector.MySqlDataReader.GetString(Int32 ordinal) in /_/src/MySqlConnector/MySqlDataReader.cs:line 283
   at lambda_method2(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at Program.<Main>$(String[] args) in /Users/homerokzam/Projects/KLog/webapi-develop/KLog.Migration/Program.cs:line 24

MySQL version: docker (latest) Operating system: MacOS with docker Pomelo.EntityFrameworkCore.MySql version: 6.0.0 Microsoft.AspNetCore.App version: net6.0

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 15 (4 by maintainers)

Most upvoted comments

Do you have nullable reference types enabled in your csproj file? If so, you’ll need to change your model correspondingly.

Is this something you want to implement upstream or does it hurt performance?

I’ve opened https://github.com/mysql-net/MySqlConnector/issues/1135 to track this. Part of the acceptance criteria will be proving that it doesn’t regress performance.

@SpicyCatGames Please post some simple model classes with accompanying Fluent API calls (if necessary) and the code with the query you are trying to run, so we can reproduce the issue. Thanks!

nah, it’s not an issue. I set IsRequired() on a column that is nullable. So there would be that error when a null value was present.

For future readers. I used the following steps to come closer to a nullability model for our EF model. Turns out we had a few properties that were nullable, but should never be, so this actually fixed a few bugs with us 😃

  1. Add a migration
  2. The new migration will change a lot of stuff, probably
  3. We use git for versioning, so I could observe the changes in the *ModelSnapshot.cs file.
  • See all changes, and note the places where a property changes type or constraints (f.ex. add .IsRequired())
  • Evaluate each property, if the field should be nullable, change it in your model
  • Sometimes, this means references/relationships need to allow null
  • If the field is meant to be non-nullable, leave it be
  1. Undo the changes to the *ModelSnapshot.cs file, and remove the migration files
  2. Repeat step 1, until satisfied