Using Async Await keywords with Dapper
Here are some examples.
Examples for Dapper - Async calls
However, this is not awaitable:
var results = await Connection.QueryAsync<T>(sql).Result.ToArray();
You have to write something like this:
var results = await Connection.QueryAsync<T>(sql);
return results.ToArray();
Here's a sample Yaron
public async Task<List<Artist>> GetAllAsync()
{
using (
SqlConnection conn =
new SqlConnection(Conn.String))
{
await conn.OpenAsync();
using (var multi = await conn.QueryMultipleAsync(StoredProcs.Artists.GetAll, commandType: CommandType.StoredProcedure))
{
var Artists = multi.Read<Artist, AlbumArtist, Artist>((artist, albumArtist) =>
{
artist.albumArtist = albumArtist;
return artist;
}).ToList();
var albums = multi.Read<Album, AlbumArtist, Album>(
(album, albumArtist, album) =>
{
album.albumArtist = album;
return albums;
}).ToList();
conn.Close();
return Artists;
}
}
}
Dapper when targeting .NET 4.5 has full support for TPL usage, via the methods ending in *Async - QueryAsync etc. Specifically, the .NET 4.5 build includes this extra set of methods