簡體   English   中英

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 編輯器客戶端(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;
    }
}

有什么想法可以正確傳遞SearchFieldSearchValue嗎?

要使用文本參數,而不是使用#>使用#>> 前者需要一個jsonb參數,而后者需要一個text參數(參見 PostgreSQL 文檔)。

您的代碼在 DataGrip 中工作的原因是'{inner_id}'是直接嵌入在 SQL 中的無類型文字,因此 PostgreSQL 將其隱式轉換為jsonb 然而,當使用 Npgsql 的參數時,Npgsql 發送一個類型化的text參數(Npgsql 的參數(幾乎)總是類型化),因此 PostgreSQL 抱怨不匹配。

添加顯式類型轉換,以便 PostgreSQL 知道字符串文字將被解釋為數組:

var rawSql = @"
            ...
            where  ""Data"" #>> CAST(@SearchField AS text[]) = @SearchValue"; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM