Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linq select cannot project an entity containing both Json columns and ICollections #30266

Closed
pella-ias opened this issue Feb 13, 2023 · 4 comments · Fixed by #30566
Closed

Linq select cannot project an entity containing both Json columns and ICollections #30266

pella-ias opened this issue Feb 13, 2023 · 4 comments · Fixed by #30566
Assignees
Labels
area-json area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@pella-ias
Copy link

Removing either the Json column or the collection from the projection solves the issue.

Minimal code to reproduce the error:

using Microsoft.EntityFrameworkCore;

var options = new DbContextOptionsBuilder<TestDbContext>().UseSqlServer("").Options;
var context = new TestDbContext(options);

var list = context.TestEntities
    .AsNoTracking()
    .Select(t => new TestEntity()
    {
        Id = t.Id,
        JsonColumn = t.JsonColumn,
        ChildEntities = t.ChildEntities,
    }).ToList();

public class TestEntity
{
    public Guid Id { get; set; }
    public JsonColumn JsonColumn { get; set; }
    public ICollection<TestChildEntity> ChildEntities { get; set; }
}

public class JsonColumn { public string Text { get; set; } }
public class TestChildEntity { public Guid Id { get; set; } }
public class TestDbContext : DbContext
{
    public DbSet<TestEntity> TestEntities { get; set; }
    public TestDbContext(DbContextOptions options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<TestEntity>().OwnsOne(e => e.JsonColumn, nav => nav.ToJson());
    }
}

Stack trace:

Unhandled exception. System.InvalidOperationException: variable '' of type 'JsonColumn' referenced from scope '', but it is not defined
   at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
   at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
   at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
   at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
   at System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)
   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
   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__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Program.<Main>$(String[] args) in \Program.cs:line 6

I've found a possibly related issue on the dotnet runtime dotnet/runtime#56262

EF Core version: 7.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net7.0 (sdk 7.0.102)
Operating system: Win 10
IDE: VSCode

@meigetsu
Copy link

Hello, I am having exactly the same issue on e project,
I confirm removing either the collection or the json column works but that's that's not an option in my situation.
Is there any known workaround for this (other then not getting one of them)?

@ajcvickers ajcvickers removed this from the 8.0.0 milestone Mar 12, 2023
@ajcvickers
Copy link
Member

@maumar Any workaround here?

@maumar
Copy link
Contributor

maumar commented Mar 20, 2023

problem comes from the bad interaction between json materializer code and the collection result coordinator. Fails for any collection (List, Array etc) not only ICollection interface.

@meigetsu @pella-ias workaround is to project entire entity and use include to bring back the related collection, rather than project the collection directly. Include uses slightly different path which is not affected by the bug.

Something like this:

        var list = ctx.TestEntities
            .AsNoTracking()
            .Include(x => x.ChildEntities)
            .ToList()
            .Select(t => new TestEntity()
            {
                Id = t.Id,
                JsonColumn = t.JsonColumn,
                ChildEntities = t.ChildEntities,
            }).ToList();

maumar added a commit that referenced this issue Mar 23, 2023
…Json columns and ICollections

Problem was that when we projection collection of entities we use result coordinator and generate different pattern in shaper code. Json entity shaper code is generated inside `resultContext.Values == null` block and all the products should be referred to via resultContext.Values array access.
We were not doing that for json, so in the final projection code we would refer to parameters (storing the actual json entities) that were out of scope.

Fix is to use the correct result coordinator pattern, just like we do for non-json entities.

Fixes #30266
maumar added a commit that referenced this issue Mar 23, 2023
…Json columns and ICollections

Problem was that when we projection collection of entities we use result coordinator and generate different pattern in shaper code. Json entity shaper code is generated inside `resultContext.Values == null` block and all the products should be referred to via resultContext.Values array access.
We were not doing that for json, so in the final projection code we would refer to parameters (storing the actual json entities) that were out of scope.

Fix is to use the correct result coordinator pattern, just like we do for non-json entities.

Fixes #30266
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Mar 23, 2023
maumar added a commit that referenced this issue Mar 24, 2023
…Json columns and ICollections

Problem was that when we projection collection of entities we use result coordinator and generate different pattern in shaper code. Json entity shaper code is generated inside `resultContext.Values == null` block and all the products should be referred to via resultContext.Values array access.
We were not doing that for json, so in the final projection code we would refer to parameters (storing the actual json entities) that were out of scope.

Fix is to use the correct result coordinator pattern, just like we do for non-json entities.

Fixes #30266
@ajcvickers ajcvickers added this to the 8.0.0 milestone Mar 24, 2023
maumar added a commit that referenced this issue Mar 24, 2023
…Json columns and ICollections

Problem was that when we projection collection of entities we use result coordinator and generate different pattern in shaper code. Json entity shaper code is generated inside `resultContext.Values == null` block and all the products should be referred to via resultContext.Values array access.
We were not doing that for json, so in the final projection code we would refer to parameters (storing the actual json entities) that were out of scope.

Fix is to use the correct result coordinator pattern, just like we do for non-json entities.

Fixes #30266
maumar added a commit that referenced this issue Mar 24, 2023
…Json columns and ICollections (#30566)

Problem was that when we projection collection of entities we use result coordinator and generate different pattern in shaper code. Json entity shaper code is generated inside `resultContext.Values == null` block and all the products should be referred to via resultContext.Values array access.
We were not doing that for json, so in the final projection code we would refer to parameters (storing the actual json entities) that were out of scope.

Fix is to use the correct result coordinator pattern, just like we do for non-json entities.

Fixes #30266
@maumar
Copy link
Contributor

maumar commented Mar 28, 2023

@ajcvickers second customer report on this issue - patch candidate or shall we wait for a few more?

@maumar maumar reopened this Mar 28, 2023
@maumar maumar modified the milestones: 8.0.0, 7.0.x Mar 29, 2023
maumar added a commit that referenced this issue Mar 30, 2023
…as regular entity collection

#30266

Problem was that when we projection collection of entities we use result coordinator and generate different pattern in shaper code. Json entity shaper code is generated inside `resultContext.Values == null` block and all the products should be referred to via resultContext.Values array access.
We were not doing that for json, so in the final projection code we would refer to parameters (storing the actual json entities) that were out of scope.
Fix is to use the correct result coordinator pattern, just like we do for non-json entities.

#30565

When we project entity collection, the shaper uses result coordinator and generates different shaper. However we process this scenario as if "regular" code path was being executed. Basically, we would generate code for collection projection outside of `resultContext.Values == null` block (this needs to happen for regular collections due to result coordination). For JSON it's not needed, because entire object is loaded from a single row. So we can move the processing into the block and then just refer to it's product in the final projection. We also need to shuffle around the order in which we build the block. We should build expressions and json entities first, then populate resultContext and then build includes (which depend on values in the context). Before we were populating result values before we generated json entities, (which didn't matter because we were not using that info) but now is necessary.

Fixes #30266
Fixes #30565
maumar added a commit that referenced this issue Apr 3, 2023
…as regular entity collection

#30266

Problem was that when we projection collection of entities we use result coordinator and generate different pattern in shaper code. Json entity shaper code is generated inside `resultContext.Values == null` block and all the products should be referred to via resultContext.Values array access.
We were not doing that for json, so in the final projection code we would refer to parameters (storing the actual json entities) that were out of scope.
Fix is to use the correct result coordinator pattern, just like we do for non-json entities.

#30565

When we project entity collection, the shaper uses result coordinator and generates different shaper. However we process this scenario as if "regular" code path was being executed. Basically, we would generate code for collection projection outside of `resultContext.Values == null` block (this needs to happen for regular collections due to result coordination). For JSON it's not needed, because entire object is loaded from a single row. So we can move the processing into the block and then just refer to it's product in the final projection. We also need to shuffle around the order in which we build the block. We should build expressions and json entities first, then populate resultContext and then build includes (which depend on values in the context). Before we were populating result values before we generated json entities, (which didn't matter because we were not using that info) but now is necessary.

Fixes #30266
Fixes #30565
ajcvickers pushed a commit that referenced this issue Apr 5, 2023
…rence as well as regular entity collection (#30600)

Fixes #30266
Fixes #30565
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-json area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
4 participants