Entity Framework 如何从不存在的类派生

huangapple go评论58阅读模式
英文:

Entity Framework how to derive from a non existent class

问题

请见谅,我不是开发人员,所以如果我没有使用正确的术语,我道歉。我正在使用代码优先的 EF 构建一个非常简单的库存管理应用程序。我有一个类,它是我的产品(为简洁起见进行了简化):

public class Product()
{
    this.Shops = new HashSet<Shop>();
}
public int Id { get; set; }
public string ProductDescription { get; set; }
public virtual ICollection<Shop> Shops { get; set; }

以及我的 Shop 类:

public Shop() 
{
    this.Products = new HashSet<Product>();
}
public int Id { get; set; }
public string ShopName { get; set; }
public virtual ICollection<Product> Products { get; set; }

并不是所有产品都在所有商店都可用,上述代码已经可以正常工作。EF 已经创建了映射表 ProductShop,我可以在任何商店中提供任何我想要的产品。

我的问题是如何处理每个商店可用的每个项目的数量。最初,我认为可以从 ProductShop 实体派生一个新类,带有类似这样的数量字段:

public class ShopStockQuantities : ProductShop
{
    public int Qty { get; set; }
}

但我没有一个物理类来派生。如果我添加了这个类,EF 会不会因为该表已经存在而出错?我相信这个问题以前已经遇到过,所以为了节省我尝试的时间(因为我不太擅长这个),我想从这些专家中寻求建议。任何建议都将不胜感激。

英文:

Please bear with me, I am not a developer, so I apologise if I don't use the correct terminology, but I am building a very simple stock management app using code first EF. I have one class which is my Products (simplified for brevity):

public class Product()
{
    this.Shops = new HashSet&lt;Shop&gt;
}
public int Id { get; set; }
public string ProductDescription { get; set; }
public virtual ICollection&lt;Shop&gt; Shops { get; set; }

And my Shop class:

public Shop() 
{
    this.Products = new HashSet&lt;Product&gt;();
}
public int Id { get; set; }
public string ShopName { get; set; }
public virtual ICollection&lt;Product&gt; Products { get; set; }

Not all products will be available at all shops, and the above works perfectly. EF has created the mapping table ProductShop and I can make whatever products I like available at any of the shops.

My problem is how to deal with the quantity of each item that is available at each shop. Initially I thought I could just derive a new class from the ProductShop entity with a quantity field like this:

public class ShopStockQuantities : ProductShop
{
    public int Qty { get; set; }
}

but I don't have a physical class to derive from. If I add that class, will EF get in a knot as that table already exists? I am sure this issue has been encountered before, so to save me hours of experimenting (as I am not very good at this anyway) I thought I would seek advice from this Borg of experts. Any advice gratefully received.

答案1

得分: 1

根据您提供的信息,这是一个可能的类结构,基本反映了Wiktor和HH所说的内容。

public class Product
{
    [Key] public int ProductId { get; set; } 
    public string ProductDescription { get; set; } = "Not Set";
}

public class Shop
{
    [Key] public int ShopId { get; set; }
    public string ShopName { get; set; } = "Not Set";
    public ICollection<Stock> StockItems { get; set; } = new Collection<Stock>();
}

public class Stock
{
    [Key] public int StockId { get; set; }
    public int ShopId { get; set; }  // 外键链接到 Shop
    public int ProductId { get; set; }   // 外键到 Product
    public int Quantity { get; set; }
}
英文:

Based on the information you've provided, this is one possible class structure which basically reflects what Wiktor and HH are saying.

public class Product
{
    [Key] public int ProductId { get; set; } 
    public string ProductDescription { get; set; } = &quot;Not Set&quot;;
}

public class Shop
{
    [Key] public int ShopId { get; set; }
    public string ShopName { get; set; } = &quot;Not Set&quot;;
    public ICollection&lt;Stock&gt; StockItems { get; set; } = new Collection&lt;Stock&gt;();
}

public class Stock
{
    [Key] public int StockId { get; set; }
    public int ShopId { get; set; }  // foreign key link to Shop
    public int ProductId { get; set; }   // foreign key to Product
    public int Quantity { get; set; }
}

huangapple
  • 本文由 发表于 2023年6月26日 15:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定