nop
nop

Reputation: 6361

EF Core: how to include an owned entity property in a non-clustered Index?

I am trying to add an owned entity property Status as an included column in a non-clustered index using EF Core. However, I am encountering the following error when creating a migration:

Unable to create a 'DbContext' of type 'AppDbContext'. The exception 'The include property 'Status' specified on the index {'TenantId', 'UpdatedOn', 'CreatedOn'} was not found on entity type 'Contract'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Here is the relevant part of my entity configuration:

public class ContractConfiguration : IEntityTypeConfiguration<Contract>
{
    public void Configure(EntityTypeBuilder<Contract> builder)
    {
        // ...

        builder.OwnsOne(o => o.Status)
            .Property(p => p.Value)
            .HasColumnName("Status")
            .HasConversion(
                v => v.ToString(),
                v => (EContractState)Enum.Parse(typeof(EContractState), v)
            ).HasMaxLength(8).IsRequired();

        // ...

        builder.HasIndex(new TenantReferenceOptions().ReferenceName, nameof(Contract.UpdatedOn), nameof(Contract.CreatedOn))
            .IsDescending(false, true, true)
            .IncludeProperties(
                nameof(Contract.LocationId), nameof(Contract.DeliveryLocationId), nameof(Contract.RegionId), nameof(Contract.ParentId), nameof(Contract.CustomerId),
                nameof(Contract.EmployeeId), nameof(Contract.CommodityId), nameof(Contract.ContractTypeId), "Status", nameof(Contract.InternalCode),
                nameof(Contract.Number), nameof(Contract.Quantity), nameof(Contract.RemainingBalance), nameof(Contract.ChildCount), nameof(Contract.Comments),
                nameof(Contract.TheirContract), nameof(Contract.GrossRemainingBalance)
            )
            .HasDatabaseName("IX_Contract_TenantId_UpdatedOn_CreatedOn");
}

Issue

Question

How can I include an owned entity property Status as an included column in an EF Core index?

I've come across this related GitHub issue: #11336, but it didn't help me.

Would appreciate any suggestions

Upvotes: 0

Views: 37

Answers (0)

Related Questions