Search code examples
c#postgresqlef-core-6.0

Save jsonb in postgres database


I have a table in Postgres with a jsonb column. I'm using Entity Framework to upsert data on this table, but I'm always getting the error

Input string was not in a correct format

because of the jsonb column.

This is an example of a query I generate:

INSERT INTO example_table (id, name, details) 
VALUES (1, 'john','{\r\n  \"age\": \"17\"\r\n}') 
ON CONFLICT (name) DO NOTHING

This is the command I'm trying to execute:

_context.ExecuteSqlRaw("INSERT INTO example_table (id, name, details) VALUES (1, 'john','{\r\n  \"age\": \"17\"\r\n}') ON CONFLICT (name) DO NOTHING");

If I remove the json the query is executed perfectly.

What am I doing wrong?


Solution

  • Json and Jsonb doesn't support \n, \r symbols. Use this:

    INSERT INTO example_table (id, name, details) 
    VALUES 
    (1, 'john','{"age":"17"}') 
    ON CONFLICT (name) DO NOTHING