Entity Framework Core jsonb column type

21,500

Solution 1

Based on H. Herzl comment:

My final solution was something like this:

public class MyTableClass
{
    public int Id { get; set; }

    [Column(TypeName = "jsonb")]
    public string Data { get; set; }
}

Migrations generated this:

Data = table.Column<string>(type: "jsonb", nullable: true),

When updated the database with migrations, the Data column was created correctly with jsonb type.

Thank you H. Herzl!

Solution 2

using string as was suggested by @bruno.almeida is a nice solution but couldn't be queried.

additional approaches are to use:

  • As System.Text.Json DOM types (JsonDocument or JsonElement)
  • As strongly-typed user-defined types (POCOs)

JsonDocument being my favorite since it could be queried, is flexible and fast to setup, e.g.:

public JsonDocument Customer { get; set; }

more details at: https://www.npgsql.org/efcore/mapping/json.html

Share:
21,500
bruno.almeida
Author by

bruno.almeida

Updated on April 09, 2021

Comments

  • bruno.almeida
    bruno.almeida about 3 years

    I am using Entity Framework Core with npgsql postgresql for Entity Framework Core.

    My question is, using migrations, how do I mark a class property to generate a JSONB column type?

    For example:

    public class MyTableClass
    {
        public int Id { get; set; }
    
        // My JSONB column
        public string Data { get; set; }
    }
    

    Thanks in advance.