编译错误
'System.Data.SqlClient.SqlConnection' 没有适用的方法名为 'Query',但似乎有一个同名的扩展方法。扩展方法无法动态分派。考虑将动态参数转换或在不使用扩展方法语法的情况下调用扩展方法。
现在,我知道如何解决问题,但我正在试图更好地理解这个错误本身。我有一个类正在构建以利用 Dapper。最终,我将提供一些更自定义的功能,以使我们的数据访问类型更加流畅。特别是构建追踪和其他东西。然而,现在它就像这样简单:
public class Connection : IDisposable
{
private SqlConnection _connection;
public Connection()
{
var connectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["ConnectionString"]);
_connection = new SqlConnection(connectionString);
_connection.Open();
}
public void Dispose()
{
_connection.Close();
_connection.Dispose();
}
public IEnumerable<dynamic> Query(string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
{
// this one works fine, without compile error, so I understand how to
// workaround the error
return Dapper.SqlMapper.Query(_connection, sql, param, transaction, buffered, commandTimeout, commandType);
}
public IEnumerable<T> Query<T>(string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
{
// this one is failing with the error
return (IEnumerable<T>)_connection.Query(sql, param, transaction, buffered, commandTimeout, commandType);
}
}
有趣的是,如果我只是说出这样一句话:
_connection.Query("SELECT * FROM SomeTable");
它编译得很好。
那么,请有人帮助我理解为什么在那些其他方法中利用相同的重载会导致出现该错误?