-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Error while querying entities of derived class with changed primary key name that have owned type in separate table #35591
Comments
@MacDon99 what is the query you are trying to execute? |
sorry, I forgot to put it in the description,
|
I'm able to reproduce this on current bits |
full simplified repro: [ConditionalFact]
public async Task Repro35591()
{
using (var ctx = new MyContext())
{
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
await ctx.Shelters.Include(_ => _.Animals).ToListAsync();
}
}
public class MyContext : DbContext
{
public DbSet<Shelter> Shelters { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Shelter>(builder =>
{
builder.ToTable("SHELTERS");
builder.HasMany(_ => _.Animals)
.WithOne()
.HasForeignKey(_ => _.ShelterId);
});
modelBuilder.Entity<Animal>(builder =>
{
builder.ToTable("ANIMALS");
});
modelBuilder.Entity<Turtle>(builder =>
{
builder.ToTable("TURTLES", tb =>
{
tb.Property(_ => _.Id).HasColumnName("TurtleId");
});
builder.OwnsOne(_ => _.Aquarium, onb =>
{
onb.ToTable("TURTLES_AQUARIUM");
});
});
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Repro35591;Trusted_Connection=True;MultipleActiveResultSets=true")
.EnableSensitiveDataLogging();
}
}
public class Shelter
{
public int Id { get; set; }
public IEnumerable<Animal> Animals { get; set; }
}
public abstract class Animal
{
public int Id { get; set; }
public int ShelterId { get; set; }
}
public class Turtle : Animal
{
public Aquarium Aquarium { get; set; }
}
public class Aquarium
{
public int Width { get; set; }
public int Length { get; set; }
public int Depth { get; set; }
} |
problem is in SelectExpression -> GenerateOwnedReferenceEntityProjectionExpression -> GetPropertyExpressions there is a discrepancy between table we are referencing (Animals, alias a) and the principal table we build columns for (Turtle). Turtle has column name override from Id to TurtleId, so we create column name TurtleId, but we reference it using alias for Animal. Moving to backlog as this is a very messy/risky code (GenerateOwnedReferenceEntityProjectionExpression) and scenario is edge case (TPT + owned types + property name overrides) |
As you're adding this to backlog, when can we expect the fix to be released? |
We don't have specific dates for issues in the Backlog, apart from it not being planned for the EF10. |
Bug description
I encountered an error while querying entities of derived class with changed primary key name that have owned type in separate table.
After some debugging I noticed that EF joins "SHELTER_MANAGER"."TURTLES_AQUARIUM" using wrong condition. As I excpect, it should join it using reference to "Turtles" table (t."TurtleId") or using reference to base animal table (a."Id") instead of mixing it.
On the other hand, changing .OwnsOne to .HasOne with simmilar configuration creates a working query:
Your code
Stack traces
Verbose output
EF Core version
8.0.12
Database provider
tested in Npgsql.EntityFrameworkCore.PostgreSQL 8.0.4 and Oracle.EntityFrameworkCore 8.23.60
Target framework
.Net 8
Operating system
Windows 10
IDE
Visual Studio 2022 17.11.5
The text was updated successfully, but these errors were encountered: