GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translate

Open
#39,833 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 2 days

@cincuranet is already working on this.

Since Nov 27, 2026.

  • #39835 by @jeremy-morren — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
csharp

Research direction

Read the GroupBy ordering expansion covered by #38918 and the association tests, especially the OwnedNavigations case; compare it with passing OwnedTableSplitting, OwnedJson and Navigations cases. Reproduce the supplied query with an owned type mapped via ToTable, then trace the translation failure. Done means the query returns posts 2 and 3 and the associated tests pass; linked pull request #39835 is already open.

Written by the indexing model from the issue text.

Description

area-groupby area-query

Ordering the elements of a group by a member of an owned type, to select the first element per group, fails when the owned type is mapped to its own table. #31062 fixed this for table-split owned types in 12.0, but the ToTable case still fails on main.

// Models
class Post
{
    public int Id { get; set; }
    public int BlogId { get; set; }
    public Timestamp Created { get; set; } = null!;
}

class Timestamp
{
    public DateTime At { get; set; }
}

// EF setup
modelBuilder.Entity<Post>().OwnsOne(p => p.Created, c => c.ToTable("PostCreated"));

// Data
context.Posts.AddRange(
    new Post { Id = 1, BlogId = 1, Created = new Timestamp { At = new DateTime(2026, 1, 1) } },
    new Post { Id = 2, BlogId = 1, Created = new Timestamp { At = new DateTime(2026, 1, 3) } },
    new Post { Id = 3, BlogId = 2, Created = new Timestamp { At = new DateTime(2026, 1, 2) } });
context.SaveChanges();

// Query: latest post per blog
var latest = context.Posts
    .GroupBy(p => p.BlogId)
    .Select(g => g.OrderByDescending(p => p.Created.At).First()) // or g.MaxBy(p => p.Created.At)
    .ToList();

Expected: posts 2 and 3.

Actual:

InvalidOperationException: Translation of 'EF.Property<Timestamp>(StructuralTypeShaperExpression(
    StructuralType: Post
    ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember
    IsNullable: False), "Created")' failed. Either the query source is not an entity type, or the specified property does not exist on the entity type.

Ordering by a property of the entity itself (g.OrderByDescending(p => p.Id).First()) works in both mappings. On main, the association tests show the same split: the query passes for OwnedTableSplitting, OwnedJson and Navigations (a regular reference navigation), and fails only for OwnedNavigations, where the owned types are mapped with ToTable. #38918 expands owned navigations in GroupBy orderings when the owned type is in the same table or in JSON, but not when it needs a join.

Dominant language
C#
Stars
14.8k
Forks
3.5k
Avg merge
3d 7h
Merged PRs (30d)
182

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.