Package Description
$ dotnet add package XO.EntityFrameworkCore.NpgsqlJsonSerializerOptionsAn Entity Framework Core plugin that adds support for JsonSerializerOptions to the Npgsql provider. You could use it to influence the property naming policy, or to use JSON source generation. These features are planned for Entity Framework Core 8.0, but until then...
Call the extension method to add the plugin to your DbContext. Optionally, configure default JsonSerializerOptions to activate the plugin for all json and jsonb columns.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseNpgsql("Host=localhost")
.UseNpgsqlJsonSerializerOptions(defaultJsonSerializerOptions: null);
}
Call UseJsonSerializerOptions to configure JsonSerializerOptions for a specific property.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>(entity =>
{
entity.Property(x => x.MyJsonProperty)
.HasColumnType("jsonb")
.UseJsonSerializerOptions(new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});
});
}