diff --git a/test/EFCore.Specification.Tests/Query/OwnedEntityQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/OwnedEntityQueryTestBase.cs index 13de6935590..1616ec4d44f 100644 --- a/test/EFCore.Specification.Tests/Query/OwnedEntityQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/OwnedEntityQueryTestBase.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; @@ -150,5 +151,84 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().OwnsMany(c => c.Names, names => names.WithOwner().HasForeignKey(n => n.ContactId)); } } + + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual async Task Projecting_owned_collection_and_aggregate(bool async) + { + var contextFactory = await InitializeAsync(); + + using var context = contextFactory.CreateContext(); + var query = context.Set() + .Select(b => new BlogDto24133 + { + Id = b.Id, + TotalComments = b.Posts.Sum(p => p.CommentsCount), + Posts = b.Posts.Select(p => new PostDto24133 + { + Title = p.Title, + CommentsCount = p.CommentsCount + }) + }); + + var result = async + ? await query.ToListAsync() + : query.ToList(); + } + + protected class MyContext24133 : DbContext + { + public MyContext24133(DbContextOptions options) + : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(blog => + { + blog.OwnsMany(b => b.Posts, p => + { + p.WithOwner().HasForeignKey("BlogId"); + p.Property("BlogId").HasMaxLength(40); + }); + }); + } + } + + protected class Blog24133 + { + public int Id { get; private set; } + + private List _posts = new(); + public static Blog24133 Create(IEnumerable posts) + { + return new Blog24133 + { + _posts = posts.ToList() + }; + } + + public IReadOnlyCollection Posts => new ReadOnlyCollection(_posts); + } + + protected class Post24133 + { + public string Title { get; set; } + public int CommentsCount { get; set; } + } + + protected class BlogDto24133 + { + public int Id { get; set; } + public int TotalComments { get; set; } + public IEnumerable Posts { get; set; } + } + + protected class PostDto24133 + { + public string Title { get; set; } + public int CommentsCount { get; set; } + } } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/OwnedEntityQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/OwnedEntityQuerySqlServerTest.cs index 79b8e622053..3f31b8ff8d3 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/OwnedEntityQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/OwnedEntityQuerySqlServerTest.cs @@ -61,5 +61,19 @@ FROM [Root24777] AS [r] INNER JOIN [Leaf24777] AS [l] ON [t].[Id1] = [l].[ModdleAId] ORDER BY [t].[Id], [t].[Id0], [t].[Id1]"); } + + public override async Task Projecting_owned_collection_and_aggregate(bool async) + { + await base.Projecting_owned_collection_and_aggregate(async); + + AssertSql( + @"SELECT [b].[Id], ( + SELECT COALESCE(SUM([p].[CommentsCount]), 0) + FROM [Post24133] AS [p] + WHERE [b].[Id] = [p].[BlogId]), [p0].[Title], [p0].[CommentsCount], [p0].[BlogId], [p0].[Id] +FROM [Blog24133] AS [b] +LEFT JOIN [Post24133] AS [p0] ON [b].[Id] = [p0].[BlogId] +ORDER BY [b].[Id], [p0].[BlogId]"); + } } }