简体   繁体   English

具有多态性 EF 核心 6 的拥有实体

[英]Owned Entity with Polymorphism EF core 6

Bonjour你好

I need some help to understand how we map OwnedEntity when they are polymorph.我需要一些帮助来理解我们 map OwnedEntity 当它们是多态时。 I have this inventory hierarchy of records that are linked to a product class.我有这个链接到产品 class 的记录的库存层次结构。

public abstract record Inventory
{
    protected Inventory() { }
}

public record NoProductInventory : Inventory
{
    public NoProductInventory()
    {
    }
}

public sealed record ProductLevelInventory : Inventory
{
    public int Stock { get; private set; } = default!;
    public int LowStock { get; private set; } = default!;
    protected ProductLevelInventory() {}
    public ProductLevelInventory(int stock, int lowStock) : base()
    {
        Stock = stock;
        LowStock = lowStock;
    }
}

public sealed record VariantLevelInventory : Inventory
{
    public int Stock { get; private set; } = default!;
    public int LowStock { get; private set; } = default!;
    public int SomeOption { get; private set; }
    protected VariantLevelInventory() {}
    public VariantLevelInventory(int stock, int lowStock) : base()
    {
        Stock = stock;
        LowStock = lowStock;
    }
}

The Product class Definition产品 class 定义

public class Product
{
    ....
    public Inventory Inventory { get; private set; } = default!;
    ....
}

I am using fluent API in order to mapped those entities我正在使用流利的 API 来映射这些实体

public class ProductEntityTypeBuilder : IEntityTypeConfiguration<Product>
{
    public void Configure(EntityTypeBuilder<Product> builder)
    {
        builder.OwnsOne(x => x.Inventory, bld =>
        {
            bld.ToTable("Inventories");
            // bld.Property(x => x.TrackInventory).IsRequired();
            bld.Property(x => x.Stock).IsRequired(false);
            bld.Property(x => x.LowStock).IsRequired(false);
            // bld.Property(x => x.InventoryTrackType).IsRequired(false);
        });
    }
}

My question is: how can I tell EF which Inventory record to use?我的问题是:如何告诉 EF 使用哪个 Inventory 记录? I don't want to use casting to figure out what kind of inventory a product has.我不想使用转换来确定产品有什么样的库存。

Good Day !再会 !

Owned entities are meant to be used as value types, containing and encapsulate multiple values which belong together and don't have a meaning on their own, ie M.netaryAmount which consists of a Amount and a Currency property.拥有的实体旨在用作值类型,包含并封装多个值,这些值属于一起并且它们自己没有意义,即M.netaryAmount ,它由一个Amount和一个Currency属性组成。

You can't do this right now.你现在不能这样做。 If you need hierarchies, you have to convert your owned types into entities and use it as regular navigation properties on the base type which are distinguished via a discriminator.如果您需要层次结构,则必须将您拥有的类型转换为实体,并将其用作通过鉴别器区分的基本类型的常规导航属性。

Owned types do not support inheritance Owned Types: Shortcommings拥有的类型不支持 inheritance 拥有的类型: Shortcommings

Current shortcomings目前的不足

Owned entity types cannot have inheritance hierarchies拥有的实体类型不能有 inheritance 个层次结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM