C# 中具有不同类型和数量的产品集/Entity Framework

Set of products with different types of products and quantity in C# / Entity Framework

我对 C# 和 Entity Framework 有点陌生,我正在尝试制作一个简单的仓库管理系统,仅供培训之用。问题是我不知道如何让成套产品发挥作用。

我有 类 这样的:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Signature { get; set; }
    public int Stock { get; set; }
}

public class SetOfProducts
{
    public int Id { get; set; }
    public string Sku { get; set; }
    // Here should be some lines of code that I don't know
}

我想做的是:

我搜索了很多,只找到了一个叫做“table association”的东西,但没有任何我能理解的细节。

我希望我解释得够多了。请帮忙:P

你想要一个 collection 导航 属性:

public ICollection<Product> Products { get; set; }

EF Core Relationships

正如 Caius Jard 所说

Seems similar; maybe should have a Kit table and a KitProduct table; 1 Kit:Many KitProduct, 1 Product:Many KitProduct. KitProduct carries qty. one product can be part of many kits. One kit can have many products. Classic M:M, Google how to handle them in EF? Common topic

回答正确。

在产品型号中我有

public virtual ICollection<ProductSetQuantity>? ProductSetQuantities { get; set; }

在 ProductSet 模型中:

public virtual ICollection<ProductSetQuantity>? ProductSetQuantities { get; set; }

还有一个名为 ProductSetQuantity 的附加关联 table table:

    public  class ProductSetQuantity
{
    [Key]
    public int Id { get; set; }

    public int? ProductId { get; set; }
    public virtual Product? Product { get; set; }

    public int? ProductSetId { get; set; }
    public virtual ProductSet? ProductSet { get; set; }

    public int Quantity { get; set; }
}