当我在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
的值。
在添加表别名时可能出现了什么问题?
1条答案
按热度按时间i2loujxw1#
在进一步阅读之后,我发现默认情况下Dapper通过名称匹配将列Map到属性,因此引入的别名将阻止这种本机行为正常工作。More on it here的数据。
仍然赞赏更多的投入在这个问题上。先谢了。