I'm new to dapper i get error when i retrieve data from joined table
当我从已连接的表中检索数据时,我对dapper有了新的错误。
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?)
“SqlConnection”不包含“查询”的定义,也不包含接受“SqlConnection”类型的第一个参数的扩展方法“查询”(您是否缺少使用指令或程序集引用?)
错误图片
1 个解决方案
#1
0
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).
虽然Dapper支持multimapping,但是调用实际上需要三个通用参数,TFirst、TSecond和TReturn(在其中TReturn可以是两种或另一种类型)。
https://github.com/StackExchange/dapper-dot-net#multi-mapping
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
或者第4个接受3个。
Query<TFirst, TSecond, TResult>( string sql, Func<TFirst, TSecond, TResult> map, ... );
In both cases you still need a map function.
在这两种情况下,您仍然需要一个map函数。
#1
0
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).
虽然Dapper支持multimapping,但是调用实际上需要三个通用参数,TFirst、TSecond和TReturn(在其中TReturn可以是两种或另一种类型)。
https://github.com/StackExchange/dapper-dot-net#multi-mapping
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
或者第4个接受3个。
Query<TFirst, TSecond, TResult>( string sql, Func<TFirst, TSecond, TResult> map, ... );
In both cases you still need a map function.
在这两种情况下,您仍然需要一个map函数。