Dapper throws exception second time you run a query if different results schema

c# coreclr dapper sqlite

Question

I'm using Dapper, Sqlite on c# coreclr.

public async Task<UserPoco> GetFromEmail(string email)
    {
        email = email.ToLower();

        using (var connection = new SqliteConnection(_configSettings.ConnectionString))
        {
            connection.Open();
            var query = $"SELECT * FROM users WHERE EmailAddress = '{email}';";
            var result = await connection.QueryAsync<UserPoco>(query);
            return result.FirstOrDefault();
        }
    }

That simple method throws an exception if at some point the SQL query find no results and next time it runs it does find results. It throws: Unable to cast object of type 'System.Int64' to type 'System.Int32'. Error parsing column 0 (UserId=86 - Int64)

I'm pretty sure it's not related to the Datatypes. The UserPoco has a long UserId property and the DB table users has a UserId of type INTEGER which is the recommended one to store bigint (long). Moreover, for example if the method always finds data it will work like a charm. I do think is related to the fact that Dapper caches the result schema of every query it runs and throws an exception when this schema changes for the same query as they point out on: https://github.com/janjosephlim/dapper-dot-net/issues/124

What I don't understand is how come I can't find more info about this or more people is complaining about such a common scenario: I'm just running a very simple query multiple times which can bring back or not results.

What am I missing?

Expert Answer

I don't know why sqlite is misreporting the data type, but: the data type checks have been improved in recent pre-release builds. Please check with the latest pre-release build: I expect it is already fixed.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why