我正在为一门课做一个项目。
我已经设置好了产品,可以将它们添加到数据库中(这是最简单的部分),所以我的问题是,将产品添加到餐饮中的最佳方法是什么。
套餐模式:
public class Meal
{
public int MealId { get; set; }
public String Name { get; set; }
public virtual Product Products { get; set; }
[Display(Name = "Creator")]
public int UserId { get; set; }
}产品型号:
public class Product
{
public int ProductId { get; set; }
public int UserId { get; set; }
public int UnitId { get; set; }
public String Name { get; set; }
public Decimal Quantity { get; set; }
public Boolean Included { get; set; }
public virtual Unit unit { get; set; }
}我想过将bool添加到产品模型中,以指示何时选择它,但不确定如何将其包含在许多不同的餐点中。我不想为每顿饭创建不同的产品,因为这似乎违背了拥有一个数据库来保存产品的目的。
不太确定如何继续。我还假设,无论我如何解决这个问题,我都可以将同样的原则应用于餐饮到餐饮计划的场景。有什么建议可以给我指明正确的方向吗?
发布于 2013-04-22 21:59:44
据我所知,您似乎在寻找两个表之间的多对多关系。我不是数据库工程师,但我认为你可能需要一个中间表,它可以作为餐饮和产品表之间的纽带。类似于:
public class ProductInMeals
{
public int ProductId { get; set; }
public int MealId { get; set; }
/* Any other fields you want to associate with this relationship */
}因此,您最终得到一个包含2个外键的表。添加一个主键和更多关于关系的细节可能是值得的,但这真的取决于您。
https://stackoverflow.com/questions/16148642
复制相似问题