dimanche 27 mars 2016

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

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 result = await connection.QueryAsync<UserPoco>($"SELECT * FROM users WHERE EmailAddress = '{email}';");
            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: http://ift.tt/1PyJhdg

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?

Aucun commentaire:

Enregistrer un commentaire