DapperLib / DapperLib/Dapper

DataException when converting SQLite nullable bigint to C# nullable int

Open
#862 3 comments 3 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'm trying to query from a SQLite table that has a nullable INTEGER column. When the first row of the result set has NULL in that column and a subsequent row does not contain NULL, Dapper throws a DataException with a message such as Error parsing column 0 (Value=1 - Int64).

Here's some example code that reproduces this bug:

using Dapper;
using Microsoft.Data.Sqlite;

void Main() {
    var cn = new SqliteConnection("DataSource=:memory:");
    var fooResult = cn.Query<Foo>(@"
    CREATE TABLE Foo ( Value INTEGER NULL );

    INSERT INTO Foo ( Value ) VALUES ( NULL );
    INSERT INTO Foo ( Value ) VALUES ( 1 );
    INSERT INTO Foo ( Value ) VALUES ( 2 );
    
    SELECT * FROM Foo;
    ");
}

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

Changing the order of the INSERT INTO lines so that the first row is not NULL does not throw an exception.

INSERT INTO Foo ( Value ) VALUES ( 1 );
INSERT INTO Foo ( Value ) VALUES ( NULL );
INSERT INTO Foo ( Value ) VALUES ( 2 );

However, changing the SELECT to explicitly cast Value to INTEGER or BIGINT returns NULL for all rows in the result set.

SELECT CAST(Value AS INTEGER) FROM Foo;

I'm using Dapper v1.50.2 and Microsoft.Data.Sqlite v1.1.1.

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

Start by running the supplied Main repro with Dapper v1.50.2 and Microsoft.Data.Sqlite v1.1.1, comparing results when the first SQLite row is NULL versus non-NULL. Trace the Query mapping and nullable Value conversion; done means both row orders map NULL, 1, and 2 to Foo.Value without a DataException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sqlite
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.