c# - Npgsql.PostgresException (0x80004005): 42883: 运算符不存在: jsonb #> text
问题描述
我有以下 SQL 查询:
select distinct "Customers"."CustomerId" as "CustomerId",
"Customers"."FcmRegistrationId" as "FcmRegistrationId",
"Customers"."FCMServerKey" as "FCMServerKey",
"Customers"."AppId" as "AppId"
from "CustomerEvents"
inner join "Customers" on "CustomerEvents"."CustomerId" = "Customers"."CustomerId"
where "Customers"."AdvertiserId" = 16 and "Data" #> '{inner_id}' = '4249699';
它在我的 SQL 编辑器客户端(DataGrip)中运行良好。但是当我使用它时C#
,我从这个问题的标题中得到了错误。
Npgsql.PostgresException (0x80004005): 42883: 运算符不存在: jsonb #> text
我将向您展示我的代码:
public class PartnerApiServices : IPartnerApiService
{
private readonly ApplicationDbContext _applicationDbContext;
public PartnerApiServices(ApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
}
public IQueryable<CustomerForPartnerApiServiceModel> GetCustomerBySearchField(Advertiser advertiser, string searchValue)
{
var rawSql = @"
select distinct ""Customers"".""CustomerId"" as ""CustomerId"",
""Customers"".""FcmRegistrationId"" as ""FcmRegistrationId"",
""Customers"".""FCMServerKey"" as ""FCMServerKey"",
""Customers"".""AppId"" as ""AppId""
from ""CustomerEvents""
inner join ""Customers"" on ""CustomerEvents"".""CustomerId"" = ""Customers"".""CustomerId""
where ""Customers"".""AdvertiserId"" = @AdvertiserId and ""Data"" #> @SearchField = @SearchValue";
var res = _applicationDbContext.BySearchFieldCustomerApiModels.FromSql(rawSql,
new NpgsqlParameter("SearchField", advertiser.SearchField),
new NpgsqlParameter("SearchValue", searchValue),
new NpgsqlParameter<int>("AdvertiserId", advertiser.AdvertiserId));
return res;
}
}
SearchField
有什么想法可以SearchValue
正确通过吗?
解决方案
使用文本参数,而不是使用#>
use #>>
。前者需要一个jsonb
参数,而后者需要一个text
参数(参见 PostgreSQL 文档)。
您的代码在 DataGrip 中工作的原因是'{inner_id}'
直接嵌入在 SQL 中的无类型文字,因此 PostgreSQL 将其隐式转换为jsonb
. 然而,当使用 Npgsql 的参数时,Npgsql 发送一个类型化text
参数(Npgsql 的参数(几乎)总是类型化),因此 PostgreSQL 抱怨不匹配。
推荐阅读
- javascript - Laravel:Ajax 动态依赖下拉列表
- python - 如何删除重复的行
- javascript - 将外部 javascript 文件添加到 React.js 应用程序
- python - 想要在独特的上下文中为矩阵的每个元素添加某个值
- javascript - 随机循环按计划工作,但在几次调用后卡在特定的随机数上
- java - 如何正确覆盖`toString()`以摆脱哈希码
- c# - 跨不同(共享)项目和命名空间的继承
- python - 如果按下多个键,Pygame 键会卡住
- swift - 如何在 swift while 循环中添加 DispatchQueue 延迟?
- visual-c++ - 获取 MIDL 2003 重新定义错误