Skip to content

EF Core 11 regression: FromSql on an entity with a JSON complex property throws NullReferenceException in GenerateComplexJsonShaper #39251

Closed

Description

@bblankenship78

Bug description

FromSql (and FromSqlRaw) on an entity type that has a JSON-mapped complex property (ComplexProperty(...).ToJson()) throws NullReferenceException from RelationalQueryableMethodTranslatingExpressionVisitor.GenerateComplexJsonShaper at query-compile time.

This is a regression in EF Core 11:

EF Core Result
10.0.13 works: OK: EF by me (5)
11.0.0-rc.1.26425.128 NullReferenceException
11.0.0-rc.2.26504.105 NullReferenceException

The same entity queried through its DbSet (no FromSql) works on 11.0. It reproduces on SQLite (below) and on PostgreSQL (Npgsql provider), so it looks provider-agnostic. My guess is that the shaper looks up the JSON column through the entity's table mapping, which is null when the root is a FromSqlExpression.

We hit it on a helper that wraps FromSql to take row locks (SELECT <mapped columns> FROM t WHERE ... FOR UPDATE) on aggregates that carry JSON complex properties. That pattern has no LINQ equivalent, so it blocks us from moving to EF Core 11.

Your code

Single-file app; run with dotnet run Repro.cs on the .NET 11 RC2 SDK. Change the package version to 10.0.13 to see it pass.

#:package Microsoft.EntityFrameworkCore.Sqlite@11.0.0-rc.2.26504.105
#:property TargetFramework=net11.0
#:property PublishAot=false

using Microsoft.EntityFrameworkCore;

await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
ctx.Blogs.Add(new Blog { Name = "EF", Details = new() { Author = "me", Rating = 5 } });
await ctx.SaveChangesAsync();
ctx.ChangeTracker.Clear();

// Works on EF Core 10.0.13; throws NullReferenceException on 11.0.0-rc.1 and 11.0.0-rc.2.
var blog = await ctx.Blogs
    .FromSql($"SELECT \"Id\", \"Name\", \"Details\" FROM \"Blogs\"")
    .SingleAsync();

Console.WriteLine($"OK: {blog.Name} by {blog.Details.Author} ({blog.Details.Rating})");

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public BlogDetails Details { get; set; } = new();
}

public class BlogDetails
{
    public string Author { get; set; } = "";
    public int Rating { get; set; }
}

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs => Set<Blog>();

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite("Data Source=fromsql-repro.db");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        => modelBuilder.Entity<Blog>().ComplexProperty(b => b.Details, d => d.ToJson());
}

Stack traces

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.GenerateComplexJsonShaper(IComplexProperty complexProperty, ITableBase table, String tableAlias, Boolean containerNullable)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.ProcessComplexProperty(IComplexProperty complexProperty, ITableBase table, String tableAlias, Boolean containerNullable)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.GenerateSingleTableSelect(IEntityType entityType, ITableBase table, TableExpressionBase tableExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.CreateSelect(IEntityType entityType, TableExpressionBase tableExpressionBase)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass11_0`1.<ExecuteCore>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo method, IQueryable`1 source, Expression additionalArgument, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo method, IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in Repro.cs:line 15

Verbose output

No response

EF Core version

11.0.0-rc.2.26504.105 (also 11.0.0-rc.1.26425.128; works on 10.0.13)

Database provider

Microsoft.EntityFrameworkCore.Sqlite (also Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0-rc.1.1)

Target framework

.NET 11.0 (SDK 11.0.100-rc.2.26504.105)

Operating system

Windows 11

IDE

dotnet CLI

Activity

  1. MihuBot commented on Oct 14, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  2. colinandres885-boop commented on Oct 16, 2026

    @colinandres885-boop

    Bug description

    FromSql (and FromSqlRaw) on an entity type that has a JSON-mapped complex property (ComplexProperty(...).ToJson()) throws NullReferenceException from RelationalQueryableMethodTranslatingExpressionVisitor.GenerateComplexJsonShaper at query-compile time.

    This is a regression in EF Core 11:

    | EF Core | Result |

    |---|---|

    | 10.0.13 | works: OK: EF by me (5) |

    | 11.0.0-rc.1.26425.128 | NullReferenceException |

    | 11.0.0-rc.2.26504.105 | NullReferenceException |

    The same entity queried through its DbSet (no FromSql) works on 11.0. It reproduces on SQLite (below) and on PostgreSQL (Npgsql provider), so it looks provider-agnostic. My guess is that the shaper looks up the JSON column through the entity's table mapping, which is null when the root is a FromSqlExpression.

    We hit it on a helper that wraps FromSql to take row locks (SELECT <mapped columns> FROM t WHERE ... FOR UPDATE) on aggregates that carry JSON complex properties. That pattern has no LINQ equivalent, so it blocks us from moving to EF Core 11.

    Your code

    Single-file app; run with dotnet run Repro.cs on the .NET 11 RC2 SDK. Change the package version to 10.0.13 to see it pass.

    #:package Microsoft.EntityFrameworkCore.Sqlite@11.0.0-rc.2.26504.105
    
    #:property TargetFramework=net11.0
    
    #:property PublishAot=false
    
    
    
    using Microsoft.EntityFrameworkCore;
    
    
    
    await using var ctx = new BlogContext();
    
    await ctx.Database.EnsureDeletedAsync();
    
    await ctx.Database.EnsureCreatedAsync();
    
    ctx.Blogs.Add(new Blog { Name = "EF", Details = new() { Author = "me", Rating = 5 } });
    
    await ctx.SaveChangesAsync();
    
    ctx.ChangeTracker.Clear();
    
    
    
    // Works on EF Core 10.0.13; throws NullReferenceException on 11.0.0-rc.1 and 11.0.0-rc.2.
    
    var blog = await ctx.Blogs
    
        .FromSql($"SELECT \"Id\", \"Name\", \"Details\" FROM \"Blogs\"")
    
        .SingleAsync();
    
    
    
    Console.WriteLine($"OK: {blog.Name} by {blog.Details.Author} ({blog.Details.Rating})");
    
    
    
    public class Blog
    
    {
    
        public int Id { get; set; }
    
        public string Name { get; set; } = "";
    
        public BlogDetails Details { get; set; } = new();
    
    }
    
    
    
    public class BlogDetails
    
    {
    
        public string Author { get; set; } = "";
    
        public int Rating { get; set; }
    
    }
    
    
    
    public class BlogContext : DbContext

    {

    public DbSet<Blog> Blogs => Set<Blog>();
    
    
    
    protected override void OnConfiguring(DbContextOptionsBuilder options)
    
        => options.UseSqlite("Data Source=fromsql-repro.db");
    
    
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    
        => modelBuilder.Entity<Blog>().ComplexProperty(b => b.Details, d => d.ToJson());
    

    }

    
    
    
    ### Stack traces
    
    
    
    

    Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.

    at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.GenerateComplexJsonShaper(IComplexProperty complexProperty, ITableBase table, String tableAlias, Boolean containerNullable)

    at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.ProcessComplexProperty(IComplexProperty complexProperty, ITableBase table, String tableAlias, Boolean containerNullable)

    at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.GenerateSingleTableSelect(IEntityType entityType, ITableBase table, TableExpressionBase tableExpression)

    at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.CreateSelect(IEntityType entityType, TableExpressionBase tableExpressionBase)

    at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitExtension(Expression extensionExpression)

    at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

    at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

    at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)

    at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query)

    at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)

    at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)

    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)

    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass11_0`1.b__0()

    at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)

    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo method, IQueryable`1 source, Expression additionalArgument, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo method, IQueryable`1 source, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)

    at Program.

    $(String[] args) in Repro.cs:line 15

    
    
    
    ### Verbose output
    
    
    
    _No response_
    
    
    
    ### EF Core version
    
    
    
    11.0.0-rc.2.26504.105 (also 11.0.0-rc.1.26425.128; works on 10.0.13)
    
    
    
    ### Database provider
    
    
    
    Microsoft.EntityFrameworkCore.Sqlite (also Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0-rc.1.1)
    
    
    
    ### Target framework
    
    
    
    .NET 11.0 (SDK 11.0.100-rc.2.26504.105)
    
    
    
    ### Operating system
    
    
    
    Windows 11
    
    
    
    ### IDE
    
    
    
    dotnet CLI
    
    
    
  3. added theissue type on Oct 16, 2026
  4. added this to the 11.0.0 milestone on Oct 16, 2026
  5. added a commit that references this issue on Oct 20, 2026
    1345b92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions