Skip to content

Weird System.DBNull behaviour on nullable columns #6379

@mhkolk

Description

@mhkolk

When dealing with nullable columns in Postgres database and those columns actually holding null values (for instance, a nullable numeric column) when reading data with NpgsqlDataReader, the values will be shown as an object (of type System.DBNull) in debugger with a value depicted as {} in Watch window.

This empty JSON string of {} will eventually and surprisingly end up downstream in calling methods when a data row is being returned as a result or if datarow is turned into a Dictionary<string, object?>.

I would expect System.DBNull to be turned into null immediately or when casting to nullable object but it doesn't happen.

We ran into this because in the end we ended with empty JSON string{}in responses from our API in places where a null should be present in that JSON.

Is the issue in any way related to this? dotnet/runtime#418 (there is no reference in my sample project - see below - to System.Text.Json)
I find this hard to believe since when at ADO level serializers shouldn't be involved, or should they?

[I published a repo with tests using TestContainers which will spin up in Docker and you can inspect/debug it.]
(https://github.com/mhkolk/NpgsqlTests)

This is the test you might want to inspect
https://github.com/mhkolk/NpgsqlTests/blob/main/NpgsqlTestProject/PostgresContainerTests.cs#L48

This is using .Net8, EF8, Npgsql 8..

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions