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<Shop>
}
public int Id { get; set; }
public string ProductDescription { get; set; }
public virtual ICollection<Shop> Shops { get; set; }
And my Shop class:
public Shop()
{
this.Products = new HashSet<Product>();
}
public int Id { get; set; }
public string ShopName { get; set; }
public virtual ICollection<Product> 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.
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; } = "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; } // foreign key link to Shop
public int ProductId { get; set; } // foreign key to Product
public int Quantity { get; set; }
}