c# - 没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换
问题描述
无法运行我的查询我的“用户”数据库上有一个“Id”,这是 postgres 的“uuid”类型,我在 dot net core 上有一个 GUID id,我希望让你的用户具有相同的 id,并返回这个用户.
public async Task<T> GetById(Guid entity)
{
try
{
connection.Open();
var result = await this.connection.QueryFirstAsync(askForId, entity);
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
connection.Close();
}
}
实体的值为:
entity: {675d83ca-c7b6-4efe-bf98-46a48e8a28fd}
我的查询是这样的:
private readonly string askForId = "SELECT * FROM Users WHERE Id = @Id";
如何解决这个问题:
"ClassName": "Npgsql.PostgresException",
"Message": "External component has thrown an exception.",
"Data": {
"Severity": "ERROR",
"InvariantSeverity": "ERROR",
"SqlState": "42883",
"MessageText": "operator does not exist: @ uuid",
"Hint": "No operator matches the given name and argument type. You might need to add an explicit type cast.",
"Position": 32,
"File": "parse_oper.c",
"Line": "731",
"Routine": "op_error"
},
}
解决方案
我将查询仅将@Id 更改为@id:
private readonly string askForId = "SELECT * FROM Users WHERE Id = @id";
并更改为QueryFirstAsync
因为 QueryFirstOrDefaultAsync
我需要知道是否找不到任何东西。
public async Task<T> GetById(Guid entity)
{
try
{
connection.Open();
var result = await this.connection.QueryFirstOrDefaultAsync<T>(askForId, new { id = entity});
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
connection.Close();
}
}
并将查询更改entity
为新的{ id = entity}
并正常工作
推荐阅读
- python - 如何将参数传递给python中的unittest类
- javascript - 导入 react-native-router-flux 时出错
- c# - Blazor, MatBlazor - 如何捕捉 MatSelect 组件的值变化
- java - Java For循环覆盖以前的元素
- xml - 迷失在命名空间中:如何访问 XSLT 中的默认命名空间项
- javascript - 如何将内联 onclick 函数编写为 es6 箭头函数?
- javascript - 我如何在 am-chart (map-chart) 中添加自定义 html 标签
- java - 如何根据yml文件生成resttemplate?
- reactjs - 使用箭头函数作为变量或函数处理 React 中的事件?
- java - Java HashMap 按键插入时间排序 - 创意顺序