-
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
Shared column with different CLR types gets projected out twice #25272
Comments
Is this issue on the roadmap for EF7? We are using TPH with a hierarchy of classes inheriting from a Unfortunately, it looks as though for every additional derived event type, the Here is a simplified example of the public abstract class Event
{
public Guid Id { get; set; }
public DateTime Created { get; set; }
}
public class StockPurchaseEvent : Event
{
public class Payload
{
public Guid StockId { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal Commission { get; set; }
}
public required Payload Content { get; set; }
}
public class StockDividendEvent : Event
{
public class Payload
{
public Guid StockId { get; set; }
public decimal DividendPerShare { get; set; }
}
public required Payload Content { get; set; }
}
// .. more event classes
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Event> Events { get; set; } = default!;
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<StockDividendEvent>()
.Property(e => e.Content)
.HasColumnType("jsonb")
.HasColumnName("Content");
builder.Entity<StockPurchaseEvent>()
.Property(e => e.Content)
.HasColumnType("jsonb")
.HasColumnName("Content");
// ... more event config
base.OnModelCreating(builder);
}
} Fetching the Events SELECT e."Id", e."Created", e."Discriminator", e."Content", e."Content", e."Content", e."Content", ... -- more duplicated columns
FROM "Events" AS e |
EF7 is released, so no. Also, while I can see that this is more of a problem is in your case than for most, this issue still only has two votes (👍) overall. We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you. |
Is there a workaround for this? It seems like inheritance in EF Core is always treated as an afterthought, never given the appropriate level of testing or support. To try and fix this, I looked into the newer ToJson functionality — surprise, it doesn't support TPH/discriminators either. This isn't a feature request; it's a glaring bug in existing functionality. The feature was released with documentation, and on paper, it should work without such blatant issues. |
Although EF shouldn't be projecting the same column multiple times, that's not a bug - at most a performance issue that's going to be pretty minor for most people; note that the issue has received only 8 votes since it was opened in 2021, which is one good reason we haven't prioritized it. I'm definitely surprised a duplicate projection serious enough to consider switching to ToJson() just because of that.
It's not broken - it's not supported yet (that's tracked by #34184). But you're right that we should be validating and throwing an informative error if the user tries to do this, and also improve our docs. |
In a TPH hierarchy, if two properties map to the same column, but have differing CLR types, the column gets projected twice.
Minimal repro
SQL:
Originally flagged by @jeuxjeux20 in npgsql/efcore.pg#1897 regarding PG JSON POCOs mapped to the same database jsonb column.
The text was updated successfully, but these errors were encountered: