我正在为一个班级做一个项目。 我正在创建一个膳食计划应用程序,允许您将产品添加到数据库,然后您选择要包含在用餐中的产品,并且可以将膳食添加到您可以按周计划查看的膳食计划中。
我已经设置了产品,可以将这些产品添加到数据库中(这很容易),所以我的问题是将产品添加到餐中的最佳方法是什么。
膳食模型:
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添加到产品模型中,以便在选择时发出信号,但不确定如何将其包含在许多不同的食物中。 我不想为每顿饭创造不同的产品,因为这似乎打败了拥有数据库来存放产品的目的。
不太确定如何进行。 我还会假设,无论我解决这个问题,我都可以将相同的原则应用于膳食计划方案。 有什么建议指出我正确的方向?
I'm working on a project for a class. I am creating a meal planner app that allows you to add products to a database then you select products to include in a meal and the meals can be added to a meal plan that you can view by a weekly schedule.
I've got the products set up and can add these to the database (that's the easy part) so my question is what is the best approach to add products to a meal.
Meal Model:
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; } }Product Model:
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; } }I thought about adding the bool to the product model to signal when it is selected but not sure how I would include that in many different meals. I didn't want to create different products for each meal as that seemed to defeat the purpose of having a database to hold products.
Not quite sure how to proceed. I would also assume that however I solve this I can apply this same principle to the meal to meal plans scenario. Any suggestions to point me in the right direction?
最满意答案
据我所知,你似乎在寻找两个表之间的多对多关系。 我不是数据库工程师,但我认为您可能需要一个中间表,可以作为膳食和产品表之间的链接。 就像是:
public class ProductInMeals { public int ProductId { get; set; } public int MealId { get; set; } /* Any other fields you want to associate with this relationship */ }所以你最终得到一个带有2个外键的表。 可能值得添加一个主键和一些关于这种关系的细节,但这完全取决于你。
From what i understand, you seem to be looking for a many to many relationship between the two tables. I'm no db engineer, but i think you probably need an intermediary table that can act as a link between the meals and products tables. Something like:
public class ProductInMeals { public int ProductId { get; set; } public int MealId { get; set; } /* Any other fields you want to associate with this relationship */ }So you end up with a table with 2 foreign keys. It may be worth adding a primary key and a few more details about the relationship, but that's really up to you.
更多推荐
发布评论