Skip to content

Index on a field of owned type #19180

Closed as not planned
Closed as not planned
@AdrianoAE-Surface

Description

@AdrianoAE-Surface

Is it possible to have a non clustered index on a field of a owned type? I have a value object that is being used in multiple entities and in one of them I want to create a index in one of the fields.

Mapped using the following implementation #15681 (comment)

Value Object:

public class TaxesPercentage : ValueObject
{
    public int Value { get; private set; }
    public decimal Multiplier { get; private set; }

    //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

    public TaxesPercentage(int value)
    {
        Value = value >= 0 && value <= 100 ? value : throw new ArgumentOutOfRangeException($"{nameof(Value)} must be between 0 and 100.");
        Multiplier = 1 + value / 100;
    }

    //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

    protected override IEnumerable<object> GetAtomicValues()
    {
        yield return Value;
        yield return Multiplier;
    }

    //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

    public override string ToString() => $"{Value}";
}

Configuration:

public class PercentageConfiguration<TEntity> : IOwnedNavigationConfiguration<TEntity, TaxesPercentage>
    where TEntity : class
{
    public void Configure(OwnedNavigationBuilder<TEntity, TaxesPercentage> percentageConfiguration)
    {
        percentageConfiguration.Property(v => v.Value)
            .HasColumnName("Percentage")
            .IsRequired();

        percentageConfiguration.Property(v => v.Multiplier)
            .HasColumnType("decimal(3,2)")
            .HasColumnName(nameof(TaxesPercentage.Multiplier))
            .HasComputedColumnSql("(CONVERT([decimal](3,2),CONVERT([decimal](3,0),[Percentage])/(100.00)+(1))) PERSISTED");
    }
}

Main entity configuration

class IvaEntityConfiguration : AuditableEntityTypeConfiguration<Iva>
{
    public override void Configure(EntityTypeBuilder<Iva> ivaConfiguration)
    {
        //...

        ivaConfiguration.ApplyOwnsOneConfiguration(i => i.Percentage, new PercentageConfiguration<Iva>());

        //────────────────────────────────────── Indexes ──────────────────────────────────────

        ivaConfiguration.HasIndex(l => l.Percentage.Value)
            .IsClustered(false)
            .HasName("IX_Iva_Percentage")
            .IsUnique();

        //─────────────────────────────────────────────────────────────────────────────────────

        base.Configure(ivaConfiguration);
    }
}

Error:
The properties expression 'l => Convert(l.Percentage.Value, Object)' is not valid. The expression should represent a simple property access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'. (Parameter 'propertyAccessExpression')

Stack trace

at Microsoft.EntityFrameworkCore.Infrastructure.ExpressionExtensions.GetPropertyAccessList(LambdaExpression propertyAccessExpression)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasIndex(Expression`1 indexExpression)
at Catalog.Persistence.Configurations.Taxes.IvaEntityConfiguration.Configure(EntityTypeBuilder`1 ivaConfiguration) in C:\Users\AdrianoEscalante\source\repos\adminbeta\Catalog.Persistence\Configurations\Taxes\IvaEntityConfiguration.cs:line 30
at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration`1 configuration)

EF Core 3.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions