MVC4应用程序最佳方法将数据库中的多个项目添加到模型中(MVC4 Application Best Approach to add multiple items from database to a

编程入门 行业动态 更新时间:2024-10-24 22:27:25
MVC4应用程序最佳方法将数据库中的多个项目添加到模型中(MVC4 Application Best Approach to add multiple items from database to a 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; } }

产品型号:

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.

更多推荐

本文发布于:2023-08-05 08:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1431135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据库中   应用程序   模型   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!