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

Query: Add regression test for #24133 #25672

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions test/EFCore.Specification.Tests/Query/OwnedEntityQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -150,5 +151,84 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Contact22089>().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<MyContext24133>();

using var context = contextFactory.CreateContext();
var query = context.Set<Blog24133>()
.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<Blog24133>(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<Post24133> _posts = new();
public static Blog24133 Create(IEnumerable<Post24133> posts)
{
return new Blog24133
{
_posts = posts.ToList()
};
}

public IReadOnlyCollection<Post24133> Posts => new ReadOnlyCollection<Post24133>(_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<PostDto24133> Posts { get; set; }
}

protected class PostDto24133
{
public string Title { get; set; }
public int CommentsCount { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
}
}
}