Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Cell value not converted when using QueryAsync<T> with CommandFlags.None

Open C0nquistadore opened this issue 2 years ago • 0 comments

I am using the QueryAsync<T> method with CommandFlags.None so it will not be buffered.

CommandDefinition command = new CommandDefinition("SELECT 1", flags: CommandFlags.None);
IEnumerable<bool> results = await connection.QueryAsync<bool>(command);
foreach (bool result in results)
{
}

Since I am not using a CAST statement, the type returned from the reader does not match the expected result type. This leads to the following exception, which is reasonable:

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Boolean'.
   at Dapper.SqlMapper.ExecuteReaderSync[T](IDataReader reader, Func`2 func, Object parameters)+MoveNext() in /_/Dapper/SqlMapper.Async.cs:line 976
   at ConsoleApp6.Program.Main(String[] args) in \ConsoleApp6\Program.cs:line 14
   at ConsoleApp6.Program.<Main>(String[] args)

However, in such cases, the value is usually converted automatically. I believe this is done by the GetValue<T> method. This works for the non-async Query method (with either buffered or non-buffered). And also for the async QueryAsync method using CommandFlags.Buffered. But apparently the GetValue<T> method is not called with the async non-buffered combination.

Is this by design or am I missing something here?

C0nquistadore avatar May 09 '23 06:05 C0nquistadore