Dapper Cast Exception on NULL value in column

Per Marc's comment this should not happen and it warrents a library fix. The issue is being tracked here and also affects other people.


Solution for sqlite nullable types. link

public class NullableLongHandler : SqlMapper.TypeHandler<long?>
{
    public override void SetValue(IDbDataParameter parameter, long? value)
    {
        if (value.HasValue)
            parameter.Value = value.Value;
        else
            parameter.Value = DBNull.Value;
    }

    public override long? Parse(object value)
    {
        if (value == null || value is DBNull) return null;
        return Convert.ToInt64(value);
    }
}

and

SqlMapper.AddTypeHandler(new NullableLongHandler());

Tags:

C#

Sqlite

Dapper