DapperLib / DapperLib/Dapper

Sqlite: InvalidCastException if the first value in a column is NULL followed by any other value

Open
#642 33 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

It seems like Dapper uses the first value in a column to decide what type the rest of the values will have.
If the first value is NULL, this fails spectacularly.

I have set up a repository demonstrating this bug:
https://github.com/CheeseSucker/DapperSqliteBug

Tested dapper versions: 1.50.0, 1.50.2, 1.50.3-beta1

Relevant code:

using (var connection = new SqliteConnection("Data Source=:memory:"))
{
    connection.Open();
    connection.Execute("CREATE TABLE MyTable (MyValue INTEGER)");
    connection.Execute("INSERT INTO MyTable (MyValue) VALUES (4)");
    connection.Execute("INSERT INTO MyTable (MyValue) VALUES (NULL)");
    connection.Execute("INSERT INTO MyTable (MyValue) VALUES (4)");

    // This is fine
    var result1 = connection.Query<MyModel>("SELECT * FROM MyTable");

    // This is also fine
    var result2 = connection.Query<MyModel>("SELECT * FROM MyTable ORDER BY MyValue IS NULL ASC");

    // This will fail because NULL is the first value in the column
    var result3 = connection.Query<MyModel>("SELECT * FROM MyTable ORDER BY MyValue IS NULL DESC");

    // InvalidCastException has been encountered before this line
    connection.Close();
}
class MyModel
{
    public long MyValue;
}

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 with the linked DapperSqliteBug repository and run the shown in-memory SQLite example, focusing on the Query call where NULL is first. Done means the query with ORDER BY MyValue IS NULL DESC completes without InvalidCastException and maps the values correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sqlite
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.