LeftTwixWand / Inventory

New version of Microsoft Inventory sample, built on WinUI 3, using DDD and Clean Architecture approach
MIT License
71 stars 10 forks source link

TPH with owned types #35

Open LeftTwixWand opened 1 year ago

LeftTwixWand commented 1 year ago

I'm trying to store events in the Warehouses table per product id. And all the events are inherited from WarehouseEventBase. But now it's impossible to have TPH (Table Per Hierarchy) with owned types, like this:

internal sealed class WarehouseEntityTypeConfiguration : IEntityTypeConfiguration<Warehouse>
{
    public void Configure(EntityTypeBuilder<Warehouse> builder)
    {
        builder.ToTable("Warehouses");

        builder.HasKey(warehouse => warehouse.ProductId);

        builder.Property(warehouse => warehouse.ProductId)
            .HasColumnName(nameof(Product.Id))
            .HasConversion(id => id.Value, value => new ProductId(value));

        builder.OwnsMany(warehouse => warehouse.DomainEvents, navigationBuilder =>
        {
            navigationBuilder.UsePropertyAccessMode(PropertyAccessMode.Field);
        });
    }
}
internal sealed class WarehouseEventBaseTypeConfiguration : IEntityTypeConfiguration<WarehouseEventBase>
{
    public void Configure(EntityTypeBuilder<WarehouseEventBase> builder)
    {
        builder.HasDiscriminator<string>(nameof(WarehouseEventBase))
            .HasValue<WarehouseCreatedEvent>(nameof(WarehouseCreatedEvent))
            .HasValue<ProductsShippedEvent>(nameof(ProductsShippedEvent))
            .HasValue<ProductsMissedEvent>(nameof(ProductsMissedEvent))
            .HasValue<ProductsReceivedEvent>(nameof(ProductsReceivedEvent));
    }
}

This behavior should be implemented as soon as the feature will be added to ef core 8. Related issue: dotnet/efcore#9630

download