Dapper
Dapper copied to clipboard
Can't use splitOn and CancellationToken with CommandDefinition
I am using the CommandDefinition in order to use the CancellationToken parameter. However, when I tried to use the splitOn, there appears to be no option for this with QueryAsync. Is there a way I can define a CommandDefinition with splitOn so I can use my CancellationToken?
Hello!
I ran into this GitHub issue earlier today when my LSP was failing me. In case this helps someone else, the splitOn paramter is an override on the Query extension methods, even if the overload uses CommandDefinition.
Here's an example using Sqlite.
init.sql:
-- $ sqlite3 demo.db -init init.sql
drop table if exists demo;
create table demo
(
foo int,
bar varchar(13),
baz int
);
insert into demo
values
( 1, 'first', 3 ),
( 2, 'second', 5 );
Program.cs:
using System.Data;
using Dapper;
using Microsoft.Data.Sqlite;
using SqliteConnection conn = new("Data Source=demo.db");
CancellationToken cancellationToken = new();
var records = await conn.QueryAsync<long, string, long, (long, string, long)>(
new CommandDefinition(
commandText: "select * from demo;",
commandType: CommandType.Text,
cancellationToken: cancellationToken),
map: (foo, bar, baz) => (foo, bar, baz),
splitOn: "bar,baz");
foreach (var record in records)
{
Console.WriteLine($"Foo: {record.Item1}; Bar: {record.Item2}; Baz: {record.Item3}");
}
Output:
$ dotnet run
Foo: 1; Bar: first; Baz: 3
Foo: 2; Bar: second; Baz: 5