Menu

PostgreSQL jsonb with EF Core 10: Mapping, Indexing and Querying Document Columns
📰

PostgreSQL jsonb with EF Core 10: Mapping, Indexing and Querying Document Columns

DEV Community: postgres·Mehdi Mohseni·2 days ago
#JepXMgOO
#devtopostgresql#devto#dev#specs#column
Reading 0:00
15s threshold

The Problem Worth Solving

You have a Product entity with fifteen optional specification fields. Half of them are null for most rows. Adding a new field means a migration. Querying by any combination of them means either a bloated WHERE clause or an EAV table that turns into a JOIN nightmare. PostgreSQL's jsonb column exists for exactly this case, and EF Core 10 with Npgsql.EntityFrameworkCore.PostgreSQL 10 maps it as a real part of the model.

This article covers the mapping, what LINQ translates to, which indexes the planner actually uses, and when a jsonb column beats a separate table. Every snippet here was run against PostgreSQL 18 with Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3, and a few things that look obvious turned out not to work. Those are called out where they come up.

Mapping: Complex Types with ToJson()

In EF Core 10 the recommended approach is to map a .NET complex type to a jsonb column with ComplexProperty(...).ToJson() .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.