Querying a TPC base type with a JSON complex property throws

Open
#39,814 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 2 days

@cincuranet is already working on this.

Since Nov 26, 2026.

  • #39815 by @jeremy-morren — open
  • #39834 by @jeremy-morren — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp, json, sql
Domain
databases

Research direction

The failure comes from GenerateJsonReader in ShaperProcessingExpressionVisitor, reached through ProcessTopLevelComplexJsonProperties; start by reading how it searches the query mappings of the abstract TPC base type. The issue suggests using the projection column from the UNION ALL instead. Done means the Set<Base>() query with a JSON complex property returns both rows, with a regression test covering it. A pull request (#39815) is already open against this issue, so check it first.

Written by the indexing model from the issue text.

Description

area-query

When a JSON complex property is declared on an abstract TPC base type, querying the base type throws while locating the JSON column.

public abstract class Base
{
    public int Id { get; set; }
    public required Document Document { get; set; }
}

public class First : Base;

public class Second : Base;

public class Document
{
    public required string Value { get; set; }
}
modelBuilder.Entity<Base>(b =>
{
    b.UseTpcMappingStrategy();
    b.ComplexProperty(r => r.Document).ToJson();
});
modelBuilder.Entity<First>().ToTable("First");
modelBuilder.Entity<Second>().ToTable("Second");
context.AddRange(
    new First { Id = 1, Document = new Document { Value = "A" } },
    new Second { Id = 2, Document = new Document { Value = "B" } });
await context.SaveChangesAsync();

var rows = await context.Set<Base>().OrderBy(r => r.Id).ToListAsync();

Expected the two rows, actual:

System.Diagnostics.UnreachableException: Could not find JSON container column 'Document' for entity type 'Base.Document#Document'.
   at ShaperProcessingExpressionVisitor.GenerateJsonReader(Int32 jsonColumnIndex, ITypeBase structuralType, IColumnBase jsonColumn)
   at ShaperProcessingExpressionVisitor.ProcessTopLevelComplexJsonProperties(...)

GenerateJsonReader looks the column up in the query mappings of the abstract base type, which isn't mapped to a table with TPC. The column of the projection (from the UNION ALL over the concrete tables) is already known at that point and could be used instead.

Saving works.

Dominant language
C#
Stars
14.8k
Forks
3.5k
Avg merge
3d 7h
Merged PRs (30d)
182

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.