编译错误
“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");
它编译得很好。
那么,有人可以帮助我理解为什么在其他方法中利用相同的重载会失败并出现该错误吗?