diff --git a/src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs b/src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs index 55741103da4..147b90ffcf7 100644 --- a/src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs +++ b/src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs @@ -3055,7 +3055,7 @@ private sealed class JsonCorrectOrderOfEntitiesForChangeTrackerValidator(SelectE { private bool _insideCollection; private bool _insideInclude; - + private SelectExpression _selectExpression = selectExpression; private readonly List<(IEntityType JsonEntityType, List<(IProperty? KeyProperty, int? ConstantKeyValue, int? KeyProjectionIndex)> KeyAccessInfo)> _projectedKeyAccessInfos = []; @@ -3212,8 +3212,11 @@ protected override Expression VisitExtension(Expression extensionExpression) { var insideCollection = _insideCollection; _insideCollection = true; + var oldSelectExpression = _selectExpression; + _selectExpression = splitCollectionShaperExpression.SelectExpression; Visit(splitCollectionShaperExpression.InnerShaper); _insideCollection = insideCollection; + _selectExpression = oldSelectExpression; return splitCollectionShaperExpression; } @@ -3231,7 +3234,7 @@ protected override Expression VisitExtension(Expression extensionExpression) ValueBufferExpression: ProjectionBindingExpression entityProjectionBindingExpression } entityShaperExpression) { - var entityProjection = selectExpression.GetProjection(entityProjectionBindingExpression).GetConstantValue(); + var entityProjection = _selectExpression.GetProjection(entityProjectionBindingExpression).GetConstantValue(); switch (entityProjection) { @@ -3270,7 +3273,7 @@ protected override Expression VisitExtension(Expression extensionExpression) } collectionResultExpression) { var collectionProjection = - selectExpression.GetProjection(collectionProjectionBindingExpression).GetConstantValue(); + _selectExpression.GetProjection(collectionProjectionBindingExpression).GetConstantValue(); switch (collectionProjection) { diff --git a/test/EFCore.Relational.Specification.Tests/Query/AdHocQuerySplittingQueryTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/AdHocQuerySplittingQueryTestBase.cs index 2b3471ebcb4..94e3b92137e 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/AdHocQuerySplittingQueryTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/AdHocQuerySplittingQueryTestBase.cs @@ -360,4 +360,104 @@ public Test(int value) } #endregion + + #region 34728 + + [ConditionalTheory] + [InlineData(true)] + [InlineData(false)] + public virtual async Task NoTrackingWithIdentityResolution_split_query_basic(bool async) + { + var contextFactory = await InitializeAsync( + onConfiguring: o => SetQuerySplittingBehavior(o, QuerySplittingBehavior.SplitQuery)); + + using var context = contextFactory.CreateContext(); + var query = context.Set() + .AsNoTrackingWithIdentityResolution() + .Select( + blog => new + { + blog.Id, + Posts = blog.Posts.Select( + blogPost => new + { + blogPost.Id, + blogPost.Author + }).ToList() + }); + + var test = async + ? await query.ToListAsync() + : query.ToList(); + } + + [ConditionalTheory] + [InlineData(true)] + [InlineData(false)] + public virtual async Task NoTrackingWithIdentityResolution_split_query_complex(bool async) + { + var contextFactory = await InitializeAsync( + onConfiguring: o => SetQuerySplittingBehavior(o, QuerySplittingBehavior.SplitQuery)); + + using var context = contextFactory.CreateContext(); + var query = context.Set() + .AsNoTrackingWithIdentityResolution() + .Select( + blog => new + { + blog.Id, + Posts = blog.Posts.Select( + blogPost => new + { + blogPost.Id, + blogPost.Author + }).ToList(), + Posts2 = blog.Posts.Select(x => new + { + x.Id, + Tags = x.Tags.Select(xx => new + { + xx.Id, + xx.Name, + xx.Name.Length + }).ToList() + }).ToList() + }); + + var test = async + ? await query.ToListAsync() + : query.ToList(); + } + + protected class Context34728(DbContextOptions options) : DbContext(options) + { + public DbSet Tests { get; set; } + + public sealed class Blog + { + public long Id { get; set; } + public string Name { get; set; } + public ISet Posts { get; set; } = new HashSet(); + } + + public sealed class BlogPost + { + public long Id { get; set; } + public WebAccount Author { get; set; } + public List Tags { get; set; } + } + + public sealed class WebAccount + { + public long Id { get; set; } + } + + public sealed class Tag + { + public int Id { get; set; } + public string Name { get; set; } + } + } + + #endregion }