为什么这个小巧的查询 return 为空?

Why does this dapper query return null?

我有一个 mysql 语句,我将其传递给小巧的查询方法,它返回一个空对象。我创建了一个 POCO class 来保存返回的结果。我直接通过 sql 编辑器测试了查询,它返回了一个结果。这是代码:

public ViewEmail GetViewEmail(string xId, int headerId)
{
    using (IDbConnection connection = new MySqlConnection(_connectionString))
    {
        string sql = "SELECT * FROM ViewEmail " +
                     "WHERE Header_id = @Header_id " +
                     "AND x_id= '@x_id'";

        var viewEmail = connection
            .Query<ViewEmail>(sql, new { Header_id = HeaderId, x_id = xId })
            .SingleOrDefault();

        return viewEmail;
    }
}

使用Prepared Statements时,不需要用'包裹参数。试试这个:

string sql = "SELECT * FROM ViewEmail " +
             "WHERE Header_id = @Header_id " +
             "AND x_id= @x_id";

Documentation