Skip to content
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

How to use IncludeProperties on HasIndex for a mix of base and derived entities using TPH? #25813

Open
joakimriedel opened this issue Sep 1, 2021 · 1 comment

Comments

@joakimriedel
Copy link
Contributor

joakimriedel commented Sep 1, 2021

I'm using TPH for a set of entities where columns of derived entities sometimes gets combined in a complex query. For this query, I need to add an index that includes these columns. I expected the following fluent API setup to work, but it doesn't.

modelBuilder.Entity<BaseEntity>()
    .HasDiscriminator(oa => oa.Kind)
    .HasValue<DerivedEntity1>(EntityKind.First)
    .HasValue<DerivedEntity2>(EntityKind.Second);
modelBuilder.Entity<BaseEntity>()
    .HasIndex(oa => new { oa.BaseProperty1, oa.BaseProperty2 })
    .IncludeProperties(oa => new { oa.BaseProperty3, (oa as DerivedEntity1).UniquePropertyForDerivedEntity1, (oa as DerivedEntity2).UniquePropertyForDerivedEntity2 });

But this fails with

The include property 'UniquePropertyForDerivedEntity1' specified on the index {'BaseProperty1', 'BaseProperty2 '} was not found on entity type 'BaseEntity'.

I've tried using string parameters instead of an expression, but get the same error.

How do I include additional columns from different derived entities into a single index without manually hacking the migration files?

Include provider and version information

EF Core version: 6 preview 5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0

@ajcvickers
Copy link
Contributor

Related to #11336.

@joakimriedel This is currently not possible. The workaround is to modify the generated migration and explicitly add the filtered columns there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants