I'm new to dapper i get error when i retrieve data from joined table
var qry = @"SELECT Cities.Id,
Cities.Name,
Cities.Sort,
Countries.Name
FROM[dbo].[Cities]
JOIN Countries ON Countries.Id = Cities.CountryId";
var result = con.Query<Cities, Countries>(qry);
this is the error message
'SqlConnection' does not contain a definition for 'Query' and no extension method 'Query' accepting a first argument of type 'SqlConnection' could be found (are you missing a using directive or an assembly reference?)
The error is caused by invalid invocation - there is no overload that accepts two generic arguments.
Event though Dapper supports multimapping, the call actually requires three generic arguments, TFirst
, TSecond
and TReturn
(where TReturn
can be one of the two or yet another type).
https://github.com/StackExchange/dapper-dot-net#multi-mapping
An example valid call from the docs
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
An invalid invocation with only two generic arguments yields exactly the error message you get.
My guess is you either wanted the 3rd overload that accepts a single generic argument
Query<TResult>( string sql, Type[] types, Func<object[], TResult> map, ... );
or the 4th one that accepts three
Query<TFirst, TSecond, TResult>( string sql, Func<TFirst, TSecond, TResult> map, ... );
In both cases you still need a map function.