1

I would like to use the new JSON Columns feature of EF Core 7 to store and retrieve data in the following format in and from my PostgreSQL database:

{
    "Name": "Email_AND_Phone_OR_RootUser",
    "Rules": [
       ["HasEmail", "HasPhone"],
       ["IsRoot"]
    ]
 }

This array of string arrays has a dynamic length and the string arrays within it too. If i understand correctly, i should create an owned class, reference it in my entity and either add the appropriate data attribute or configure it OnModelCreating. In the examples i find in the internet, i don't see any use of lists or arrays within the JSON mapping class. Are the following mapping classes valid ?

public class Policy
{
    [MaxLength(30)] public string Name { get; set; } = null!;
    public List<List<string>> RuleBinding { get; set; } = new();
}

Or, as an array of string arrays:

public class Policy
{
    [MaxLength(30)] public string Name { get; set; } = null!;
    public string[][] RuleBinding { get; set; } = null!;
}

Also, is the use of JSON Columns appropriate in this case or not?

1 Answer 1

1

EF Core library for PostgreSQL (Npgsql.EntityFrameworkCore.PostgreSQL) has it's own support for JSON which was build prior to EF Core 7 and EF 7 APIs are not supported yet - see JSON Mapping doc:

EF Core 7.0 introduced support for JSON columns. Npgsql's JSON support - detailed below - is different, and has been available since version 3.0. We plan to adopt EF's JSON support in version 8.0.

Both mapping should be supported AFAIK.

public class Policy
{
    [MaxLength(30)] public string Name { get; set; } = null!;
    [Column(TypeName = "jsonb")] public List<List<string>> RuleBinding { get; set; } = new();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer. Is there a preferable data structure to use for JsonB? Is my approach (using arrays or lists) okay or should i preferably store it as a string or a Dictionary<string, object>?
@Arkena it depends on your goals, personally I find structured approaches more suitable (you can use classes even over Dictionaryes), but not always they can be used, in some cases you can even use JsonDocument as mentioned in the docs.
The data i store as jsonb is related to policies and rules for ABAC authorization. I'm using postgres to persist the rules and policies and update them. For reads, it will be accessible from the local cache. In my services, the administrators should be allowed to add and remove rules / policies and attach them to the endpoints they see fit through the user interface. Since authorization is a blocking task that occurs at each request and since there will be some sorting and validation involved in the process i'm afraid to use a data structure that increases latency and memory usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.