Npgsql.PostgresException (0x80004005): 42883: 运算符不存在: jsonb #> text
Npgsql.PostgresException (0x80004005): 42883: operator does not exist: 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-editor-client (DataGrip) 中运行良好。但是当我将它与 C#
一起使用时,我从这个问题的标题中得到了错误。
Npgsql.PostgresException (0x80004005): 42883: operator does not exist: 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
吗?
要使用文本参数,请使用 #>>
而不是 #>
。前者需要一个 jsonb
参数,而后者需要一个 text
参数 (see the PostgreSQL docs).
您的代码在 DataGrip 中工作的原因是 '{inner_id}'
是直接嵌入到您的 SQL 中的无类型文字,因此 PostgreSQL 将其隐式转换为 jsonb
。但是,当使用 Npgsql 的参数时,Npgsql 发送类型化的 text
参数(Npgsql 的参数(几乎)总是类型化的)),因此 PostgreSQL 抱怨不匹配。
添加显式类型转换,以便 PostgreSQL 知道字符串文字将被解释为数组:
var rawSql = @"
...
where ""Data"" #>> CAST(@SearchField AS text[]) = @SearchValue";
我有以下 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-editor-client (DataGrip) 中运行良好。但是当我将它与 C#
一起使用时,我从这个问题的标题中得到了错误。
Npgsql.PostgresException (0x80004005): 42883: operator does not exist: 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
吗?
要使用文本参数,请使用 #>>
而不是 #>
。前者需要一个 jsonb
参数,而后者需要一个 text
参数 (see the PostgreSQL docs).
您的代码在 DataGrip 中工作的原因是 '{inner_id}'
是直接嵌入到您的 SQL 中的无类型文字,因此 PostgreSQL 将其隐式转换为 jsonb
。但是,当使用 Npgsql 的参数时,Npgsql 发送类型化的 text
参数(Npgsql 的参数(几乎)总是类型化的)),因此 PostgreSQL 抱怨不匹配。
添加显式类型转换,以便 PostgreSQL 知道字符串文字将被解释为数组:
var rawSql = @"
...
where ""Data"" #>> CAST(@SearchField AS text[]) = @SearchValue";