📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

When would you store data in a JSON column with EF Core instead of separate tables, and how do you index it on SQL Server?

Mid-Level Theory Intermediate

🎯 Key points

  • JSON for owner-scoped, read-together, rarely-queried-alone data
  • Tables for joins, constraints, cross-row aggregation, concurrent partial updates
  • EF Core ToJson() on complex or owned types; queries translate to JSON_VALUE/OPENJSON
  • Filtering inside JSON parses every document unless indexed
  • Index: computed column over JSON_VALUE + normal index
  • EF Core 10 maps to the native json type on Azure SQL and SQL Server 2025
  • No FK, unique or check constraints inside the document

⏱ 30-second answer

Use a JSON column (ComplexProperty(...).ToJson() or OwnsOne/OwnsMany(...).ToJson()) for data that is owned by one row, read and written with it, and rarely queried alone: preferences, a variable set of attributes, a snapshot of an address at order time. Use tables when you need to join, filter or aggregate across rows at scale, enforce foreign keys, unique or check constraints, or update parts concurrently. Performance trade-off: reading the whole document with its owner is cheap (one row, no join), but filtering inside JSON means parsing every document unless you index it. On SQL Server, index a property by adding a computed column over JSON_VALUE(Data, '$.Country') and indexing that column; it works on every supported version. EF Core 10 can map to the native json data type on Azure SQL and SQL Server 2025, which stores documents in a binary form that is faster to read; check your target's support for dedicated JSON indexes before relying on them.

🗣 Full answer (senior)

How I would answer this in an interview

"I ask how the data is used. If it always travels with its owner, has a shape that varies, and nobody queries it on its own, JSON is a good fit: user preferences, a product's optional attributes, the shipping address as it was when the order was placed. One row, one read, no join, and the schema can evolve without a migration per attribute.

As soon as I need to join to it, enforce a foreign key or uniqueness, aggregate across many rows, or let two users update different parts at the same time, it belongs in tables. A JSON document is a single value to the database: constraints can't reach inside it and any update rewrites the whole thing.

In EF Core I map it with ToJson(), and queries like Where(c => c.Preferences.Country == \"AU\") translate to JSON_VALUE. Without an index that parses every row. On SQL Server the reliable way to index is a persisted or non-persisted computed column over JSON_VALUE with an ordinary index on it; the optimiser matches the expression. EF Core 10 also maps to the native json type on Azure SQL and SQL Server 2025, which avoids reparsing text and is the default I'd pick on those targets."


modelBuilder.Entity<Customer>().ComplexProperty(c => c.Preferences, p => p.ToJson());
ALTER TABLE dbo.Customers ADD PrefCountry AS JSON_VALUE(Preferences, '$.Country');
CREATE INDEX IX_Customers_PrefCountry ON dbo.Customers(PrefCountry);

JSON vs tables

Need JSON column Tables
Read with owner One row, fast Join
Filter across rows Needs computed-column index Normal indexes
Constraints and FKs No Yes
Schema changes No migration per attribute Migration
Concurrent partial updates Whole document rewritten Row-level

Production example

Moving [25] sparse product attribute columns into one JSON column removed a [mostly-NULL] wide table; the one attribute used in search got a computed-column index and kept its query under [10 ms].

🔍 What the interviewer is checking

Checks storage judgement for JSON mapping.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me