db2 为什么添加表别名时Dapper失败?

rryofs0p  于 2023-08-05  发布在  DB2
关注(0)|答案(1)|浏览(194)

当我在Dapper中执行一个没有表别名的查询时,它没有任何问题,如下所示:

SELECT * FROM ApplicationUsers
WHERE EmailAddress LIKE '%' || @EmailAddress || '%'
AND CreatedAt >= @CreatedStart
AND CreatedAt <= @CreatedEnd
ORDER BY CreatedAt DESC
LIMIT @Limit OFFSET @Offset

字符串
C#:

return await connection.QueryAsync<ApplicationUser>(sql, filter);


答案如下:

{
  "success": true,
  "result": {
    "users": [
      {
        "id": 2,
        "firstName": "Amjad",
        "lastName": "Aj",
        "name": "Amjad Aj",
        "role": "Admin",
        "emailAddress": "amjad@aj.com",
        "username": "amjad",
        "isEnabled": true,
        "lastLogin": "2023-07-19",
        "created": "2023-07-19"
      }
    ]
  }
}


但是,当我向ApplicationUsers添加别名u时,它失败了。
查询:

SELECT * FROM ApplicationUsers u
WHERE u.EmailAddress LIKE '%' || @EmailAddress || '%'
AND u.CreatedAt >= @CreatedStart
AND u.CreatedAt <= @CreatedEnd
ORDER BY u.CreatedAt DESC
LIMIT @Limit OFFSET @Offset


我得到下面的回应:

{
  "success": false,
  "result": {
    "message": "SQLite Error 1: 'no such column: u.EmailAddress'."
  }
}


我尝试在连接到SQLite以外的数据库(如IBM Db2)时使用表别名,但仍然得到类似的错误:<column-name> is not valid in the context where it is used的值。
在添加表别名时可能出现了什么问题?

i2loujxw

i2loujxw1#

在进一步阅读之后,我发现默认情况下Dapper通过名称匹配将列Map到属性,因此引入的别名将阻止这种本机行为正常工作。More on it here的数据。
仍然赞赏更多的投入在这个问题上。先谢了。

相关问题