Skip to content

Filter on JSON complex property over a LEFT JOIN drops unmatched rows #39523

Description

@jeremy-morren

Filter on JSON complex property over a LEFT JOIN drops unmatched rows

When an entity with a JSON complex property (ComplexProperty(...).ToJson()) is on the optional side of a LEFT JOIN, filtering on a property of that complex type loses the null compensation. Rows with no match are dropped.

Models

public class Blog
{
    public int Id { get; set; }
    public Details Details { get; set; }
}

public class Details
{
    public string City { get; set; }
}

modelBuilder.Entity<Blog>()
    .ComplexProperty(b => b.Details)
    .ToJson();

context.Blogs.AddRange(
    new Blog { Id = 1, Details = new Details { City = "Sydney" } },
    new Blog { Id = 2, Details = new Details { City = "Berlin" } });
await context.SaveChangesAsync();

var ids = await (
        from o in context.Blogs
        join n in context.Blogs on o.Id + 1 equals n.Id into next
        from n in next.DefaultIfEmpty()
        where n!.Details.City != "Berlin"
        select o.Id)
    .ToListAsync();

Result

  • Expected: [2]. Blog 2 has no next blog, so n is null and the predicate is true.
  • Actual: []

The IS NULL check is missing from the SQL:

-- Expected
WHERE JSON_VALUE([b0].[Details], '$.City') <> N'Berlin' OR JSON_VALUE([b0].[Details], '$.City') IS NULL

-- Actual
WHERE JSON_VALUE([b0].[Details], '$.City') <> N'Berlin'

SQLite behaves the same ("b0"."Details" ->> 'City' <> 'Berlin'). So does a subquery as the inner source, e.g. context.Blogs.OrderBy(b => b.Id).Take(5).

EF Core main (12.0.0-alpha), SQL Server and SQLite.

Activity

  1. added this to the 11.0.1 milestone on Nov 5, 2026
  2. added theissue type on Nov 5, 2026
  3. cincuranet commented on Nov 5, 2026

    @cincuranet
    Contributor

    Regression in 11.

    10.0.14:

          SELECT "b"."Id"
          FROM "Blogs" AS "b"
          LEFT JOIN "Blogs" AS "b0" ON "b"."Id" + 1 = "b0"."Id"
          WHERE "b0"."Details" ->> 'City' <> 'Berlin' OR "b0"."Details" ->> 'City' IS NULL

    11.0.0-rc.3.26522.148:

          SELECT "b"."Id"
          FROM "Blogs" AS "b"
          LEFT JOIN "Blogs" AS "b0" ON "b"."Id" + 1 = "b0"."Id"
          WHERE "b0"."Details" ->> 'City' <> 'Berlin'
  4. cincuranet commented on Nov 5, 2026

    @cincuranet
    Contributor

    AI Triage

    The below is an AI-generated analysis and may contain inaccuracies.

    Issue: #39523
    Mode: Normal. Safety scan completed on the issue, full discussion, and linked PR material. Reconstructed the inline case in an isolated console project, using synthetic data and SQLite in-memory only; no reporter-supplied project, scripts, or external database were used.

    Recommendation: KEEP — high confidence. This is a confirmed incorrect-results regression between the tested EF Core 10 and 11 builds, also present in the tested 12 alpha. This independently corroborates the existing maintainer comment. Retain issue type Bug, regression, area-query, and area-json; add area-complex-types. No provider label is warranted for a shared relational-pipeline problem reported on both SQL Server and SQLite. Leave the existing 11.0.1 milestone unchanged.

    Reproduction and regression evidence

    Executed the same minimized query and model on Windows x64, targeting net11.0, using SDK 11.0.100-rc.1.26420.103 and runtime .NET 12.0.0-alpha.1.26456.102 (DOTNET_ROLL_FORWARD=LatestMajor). The SQLite engine was held at 3.53.4, with SQLitePCLRaw.bundle_e_sqlite3 pinned to 3.0.5 for every run. EF Core and Microsoft.EntityFrameworkCore.Sqlite versions were matched:

    EF Core / provider Actual IDs Null compensation
    10.0.14 [2] — correct OR "b0"."Details" ->> 'City' IS NULL present
    11.0.0-rc.3.26522.148 [] — incorrect Missing
    12.0.0-alpha.1.26473.114 [] — incorrect Missing

    For each version, dotnet build .\Repro.csproj -p:EfVersion=<version> --nologo --verbosity quiet succeeded, then dotnet .\bin\Debug\net11.0\Repro.dll executed successfully and produced the results above. The project references Microsoft.EntityFrameworkCore.Sqlite at $(EfVersion) and the pinned SQLite bundle. These are execution results, not just ToQueryString() inspection.

    This establishes the regression interval, not the introducing commit or first affected preview. SQL Server, nested properties, and the Take subquery variant were not independently executed in this triage. The tested 12 alpha is not a build of current main.

    minimal repro
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    
    await using var context = new TestContext();
    await context.Database.OpenConnectionAsync();
    await context.Database.EnsureCreatedAsync();
    context.Blogs.AddRange(
        new Blog { Id = 1, Details = new Details { City = "Sydney" } },
        new Blog { Id = 2, Details = new Details { City = "Berlin" } });
    await context.SaveChangesAsync();
    
    var ids = await (
        from o in context.Blogs
        join n in context.Blogs on o.Id + 1 equals n.Id into next
        from n in next.DefaultIfEmpty()
        where n!.Details.City != "Berlin"
        select o.Id).ToListAsync();
    Console.WriteLine($"Result: [{string.Join(", ", ids)}]");
    
    public class TestContext : DbContext
    {
        public DbSet<Blog> Blogs => Set<Blog>();
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlite("Data Source=:memory:")
                .LogTo(Console.WriteLine, LogLevel.Information);
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
            => modelBuilder.Entity<Blog>().ComplexProperty(b => b.Details).ToJson();
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public Details Details { get; set; } = null!;
    }
    
    public class Details
    {
        public string City { get; set; } = null!;
    }

    Source assessment and existing fix

    The inspected source supports the proposed diagnosis:

    The existing open PR #39524 directly addresses that gap by calling JsonQueryExpression.MakeNullable(). Its diff includes direct-property, nested-property, and left-joined Take subquery regression cases, with SQLite and SQL Server baselines. It is not merged and no released fix is established. At inspection, SQLite and SQL Server checks passed, while the Main checks failed on all three platforms; their causes were not investigated here. The patch itself was not executed locally.

    Duplicate search and next steps

    Searched open and closed EF Core issues for "JSON" "null compensation", "complex" "LEFT JOIN", "JsonScalarExpression" "nullable", and "JSON" "compensation" "join". No confirmed duplicate was found. Related but distinct:

    Proceed with review of #39524, resolve or explain its failing Main checks, and confirm the three regression cases on the intended 11.0.1 servicing branch as well as main. No additional reporter information is needed for the reproduced direct-property case. This triage does not close the issue or change its assignment or milestone.

  5. modified the milestones: 11.0.1, 11.0.2 on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions