Indexes and Owned Types

ef-core-2.0 entity-framework-core

Question

I have searched and searched, and I believe I know the answer. But I'm asking, just to make sure...

I have been creating Owned Types in my EF Core 2.0 models, and working through the... quirks so far. My current issue is creating an index that includes one or more properties from my owned type inside of another entity.

Essentially, as far as I can tell, the owned type is running into the same issue as trying to index on a navigation property (and, BTW, awesome job MS choosing exception text that tells you nothing about the actual problem...). However, with navigation properties, you can resort to the string-name version and reference the shadow property. I have had no luck trying that with owned types - it appears EF doesn't create "shadow properties" for them. Referencing the actual column name errors out because it's not a defined property name on the model (and would appear to confirm my "no shadow properties" thought); and nameof doesn't work with sub-objects, and would probably give the wrong name anyway.

So what I have resorted to is creating indexes with the Fluent API using as many of the columns as I can, and then adding the additional columns I need directly in the migration script. While it works, this is, for reasons I hope are obvious, sub-optimal.

So what am I missing? I really want to use owned types, but given the "Vortex Of Renaming Death" on migrations and now this, I'm getting close to saying "screw it", pulling them out, and re-doing them manually, if for no other reason than to avoid ire from the rest of my team. Obviously this is a relatively new feature, but I am struggling to understand why it was released in the state it's in...

1
4
3/13/2018 6:03:17 PM

Popular Answer

you can define index for owned types, tried with EF core 2.1 preview 2

public class MainEntityTypeConfiguration : IEntityTypeConfiguration<MainEntity>
{
    public void Configure(EntityTypeBuilder<MainEntity> builder)
    {
        builder.ToTable(nameof(MainEntity));
        builder.Ignore(b => b.DomainEvents);

        builder.HasKey(x => x.MainEntityId);
        builder.HasIndex(x => x.AnyPropFromMainEntity);

        builder.OwnsOne(x => x.ValueObjectInsideMainEntity, xx =>
        {
            xx.HasIndex(o => o.ValueObjectsProp);
            xx.Property(o => o.ValueObjectsProp2).HasMaxLength(256);
        });
    }
}
6
4/25/2018 6:31:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow