Skip to content

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

In review

Description

@jeremy-morren

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.

Activity

  1. added a commit that references this issue on Nov 27, 2026
    87ec5f5
  2. added theissue type on Nov 27, 2026
  3. self-assigned this
    on Nov 27, 2026
  4. added this to the 12.0.0 milestone on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions