使用 Dapper ORM (.NET Core) 将 JSON 插入 PostgreSQL 数据库

Insert JSON into PostgreSQL database with Dapper ORM (.NET Core)

标题几乎解释了它。其他一切正常,我可以 INSERT/UPDATE/DELETE/SELECT 从我的应用程序使用 Dapper 没有问题。我遇到的具体问题是在尝试将 INSERT 值放入我的 table 时。

我尝试了几种不同的方法,但似乎找不到一种可以将 JSON 值正确插入我的 PostgreSQL 数据库的方法。我总是收到错误消息,我试图在数据库需要 JSON.

的地方插入类型 TEXT 的值

为了简洁起见,我将尽量只包括相关的代码片段。

以下 RAW SQL 在 pgAdmin 中工作得很好:

INSERT INTO public.sales(
     firstname, lastname, middlename, address1, address2, city, state, 
    zipcode, dateofbirth, phonenumber, phonenumberalt, insurancename, insuranceid, binnumber, pcnnumber, groupid, 
    offerid, offercodes, timestamp, otherfields)
    VALUES ('Fname', 'lname1', NULL, '123 ABC St', NULL, 'Washington DC', 'DC', 
            '10062', '1988-01-01', '9545555555', NULL, 'BCBS', 'XYZ123', '600100', 'ABC123', 'DC123', 
            '12', '10,50,12', '2017-03-24', '{     "guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a",     "name": "Angela Barton",     "is_active": true,     "company": "Magnafone",     "address": "178 Howard Place, Gulf, Washington, 702",     "registered": "2009-11-07T08:53:22 +08:00",     "latitude": 19.793713,     "longitude": 86.513373,     "tags": [         "enim",         "aliquip",         "qui"     ] }');

格式有点不对——但你可以看到我基本上只是在一个字符串中有 JSON object 并且它插入得很好。但是,当我尝试通过我的应用程序执行我认为完全相同的操作时(使用 NancyFX & Dapper)。

这是数据 access/repository 代码以及显示正在发生的事情(和错误)的屏幕截图:

public void Add(Sale item)
{
    string sQuery = "INSERT INTO Sales (firstname, lastname, middlename, address1, address2, city, state, zipcode, dateofbirth, phonenumber, phonenumberalt, insurancename, insuranceid, binnumber, pcnnumber, groupid, offerid, offercodes, timestamp, otherfields)"
                    + " VALUES(@FirstName, @LastName, @MiddleName, @Address1, @Address2, @City, @State, @ZipCode, @DateOfBirth, @PhoneNumber, @PhoneNumberAlt, @InsuranceName, @InsuranceId, @BinNumber, @PcnNumber, @GroupId, @OfferId, @OfferCodes, @Timestamp, '@OtherFields')";
    using(IDbConnection active = dbConn) 
    {
        active.Open();
        active.Execute(sQuery, item);
    }
}

如您所见,该程序似乎正在尝试执行我在 pgAdmin 中手动执行的完全相同的操作。我的猜测是 Dapper 强制类型或告诉 postgre string 对应于 text 这就是我收到错误的原因?

我唯一的问题也是经过一番搜索后,我不确定如何解决这个问题。

这是重要的一点:"<pre>Nancy.RequestExecutionException: Oh noes! ---&lt; Npgsql.PostgresException: 22P02: invalid input syntax for type json

这里还有一张包含完整消息的屏幕截图:

所以,谁能指出我正确的方向?我是否需要将我的 "OtherFields" 作为已经序列化的 JObject 传递才能使其正常工作?我基本上已经尝试了 SQL 语法中我能想到的所有内容,包括强制转换为 json (::json) 和其他一些我什至现在都记不起来的事情。

作为进一步的健全性检查:如果我从 Dapper 的查询字符串中完全删除 "OtherFields",那么之前完全相同的请求就可以正常工作(具有讽刺意味的是 returns "OtherFields" 由于我写 Module 的方式)。

TL;DR - 我需要使用 Dapper 将 JSON 值插入到我的 PostgreSQL 数据库中。哈尔普

欢迎任何help/suggestions!谢谢

我觉得自己好傻...

所以我以为我已经尝试了所有可能的转换语法,但显然没有。

在下面的代码中:

public void Add(Sale item)
{
    string sQuery = "INSERT INTO Sales (firstname, lastname, middlename, address1, address2, city, state, zipcode, dateofbirth, phonenumber, phonenumberalt, insurancename, insuranceid, binnumber, pcnnumber, groupid, offerid, offercodes, timestamp, otherfields)"
                    + " VALUES(@FirstName, @LastName, @MiddleName, @Address1, @Address2, @City, @State, @ZipCode, @DateOfBirth, @PhoneNumber, @PhoneNumberAlt, @InsuranceName, @InsuranceId, @BinNumber, @PcnNumber, @GroupId, @OfferId, @OfferCodes, @Timestamp, '@OtherFields')";
    using(IDbConnection active = dbConn) 
    {
        active.Open();
        active.Execute(sQuery, item);
    }
}

你可以看到我的 @OtherFieldsjson 类型的列。如果您尝试使用 '@OtherFields'::json 进行转换,您仍然会收到来自 npgsql 的完全相同的错误。 HOWEVER,如果您删除单引号(例如 @OtherFields::json),完全相同的 API 请求现在可以正常工作。

我会保留它,因为我仍然对传递已经序列化的对象的能力有点好奇,如果我使用强制转换运算符或者我会必须 DESERIALIZE JObject 值转换为 string 并使用这里显示的转换。

如果有人可以解释and/or以上小问题的答案,我会将你的答案标记为"the"答案。

再次感谢!

不同之处在于,在 pgAdmin 中,您发送 '{}' 而您的代码发送 '"{}"',这是无效的 json。