Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am a newbie to PostgreSQL db and we're trying to implement an endpoint to save JSON data to PostgreSQL DB using the .NET Core 2.0 api's

# PostgreSQL Table Definition:
ID - UUID
DeptId - int4
DeptSettings - json

Can anyone please share a code sample to save json data to PostgreSQL using Entity Framework in .NET Core/C#?

Thank you in advance.

What I have tried:

Unable to find any solution for the same over internet. Henceforth, reaching out to broader forum.
Posted
Updated 21-Jul-18 16:31pm
v2

1 solution

Json type is mapped to string in .Net. So, define DeptSettings as string in entity model.
C#
public class Department {
  public Guid Id { get; set; }
  public int DeptId { get; set; }
  public string DeptSettings { get; set; }
}

In the ApplicationDbContext file, explicitly specify the PostgreSQL data type by adding the following to your model's OnModelCreating:
C#
protected override void OnModelCreating(ModelBuilder builder) {
  builder.Entity<Department>().Property(d => d.DeptSettings).HasColumnType("json");
}

This way, the string will be cast to json and stored in the database.

Note: The value of the DeptSettings must be a string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900