DapperLib / DapperLib/Dapper

Error parsing null value into nullable property

Open
#1,042 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

I have a wrapper around IDataReader that returns null when the value is DBNull, like this:

public class MyDataReaderWrapper : IDataReader
{
    public object GetValue(int index)
    {
        if (_wrappedDataReader.IsDBNull(index)) return null;
        ...
    }
}

When using the following code Dapper gives "Error parsing column 0(Value=NULL)".

public class Test
{
    public int? Value { get; set; }
}

...

var test = _wrappedDBConnection.Query<Test>("SELECT NULL AS Value FROM DUAL");

I understand that Dapper already deals with DBNull, but my application uses this wrapper so that we don't have to deal with DBNull when using IDataReader directly.

I think Dapper should still work when mapping a null value to a nullable property.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with MyDataReaderWrapper, IDataReader.GetValue, the nullable Test.Value property, and the Query call shown in the report. Trace Dapper's column-mapping path for a null object value and compare it with DBNull handling. Done means the query maps the wrapper's null to int? Value without an error, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.