Skip to content

Querying a table-splitting dependent of a derived TPH type returns rows of other types #39458

Closed

Description

@jeremy-morren

Bug description

CatProfile shares the Animal table with Cat, a derived type in a TPH hierarchy. context.Set<CatProfile>() returns a profile for every row in the table, including the Dog row, which has no profile:

SELECT "a"."Id", "a"."Note"
FROM "Animal" AS "a"

Expected: only the profiles of Cat rows. Loading them through Cat.Profile is correct.

Related: #38659 (same model shape, but materializing the base type).

This is also why #39448 rejects JSON complex properties on dependents of sibling types mapped to the same column: Set<CatProfile>() would return the Dog's JSON.

Your code

public abstract class Animal
{
    public int Id { get; set; }
    public string Name { get; set; } = null!;
}

public class Dog : Animal;

public class Cat : Animal
{
    public CatProfile? Profile { get; set; }
}

public class CatProfile
{
    public int Id { get; set; }
    public string? Note { get; set; }
}

modelBuilder.Entity<Animal>(); // TPH
modelBuilder.Entity<Dog>();
modelBuilder.Entity<Cat>(b =>
{
    b.HasOne(c => c.Profile)
        .WithOne()
        .HasForeignKey<CatProfile>(p => p.Id);
    b.Navigation(c => c.Profile)
        .IsRequired();
});
modelBuilder.Entity<CatProfile>()
    .ToTable("Animal");
context.AddRange(
    new Dog { Name = "Rex" },
    new Cat { Name = "Tom", Profile = new CatProfile { Note = "cat" } });
await context.SaveChangesAsync();

var profiles = await context.Set<CatProfile>().ToListAsync(); // 2 profiles: Tom's, and one for Rex

EF Core version

7.0.0 and later (checked 7.0.0, 8.0.0, 9.0.0, 10.0.14, 11.0.0-rc.3). Regression: 6.0 joined the principal and filtered by its discriminator:

SELECT "a"."Id", "a"."Note"
FROM "Animal" AS "a"
INNER JOIN (
    SELECT "a0"."Id"
    FROM "Animal" AS "a0"
    WHERE "a0"."Discriminator" = 'Cat'
) AS "t" ON "a"."Id" = "t"."Id"

Providers: all relational.

Activity

  1. MihuBot commented on Oct 29, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  2. added this to the Backlog milestone on Oct 30, 2026
  3. added theissue type on Oct 30, 2026
  4. added a commit that references this issue on Nov 4, 2026
    a97b0c7
  5. added a commit that references this issue on Nov 13, 2026
    4fd472a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions