将权重存储在数据库表中
我正在学习 MVC,想要创建一个菜谱记录器应用程序来存储我的菜谱。
我正在将 .net 与 Sql Server 2008 R2 一起使用,但我认为这对我想要做的事情并不重要。
我希望能够记录我使用的所有措施。在我的国家,我们使用公制,但我希望人们能够在我的应用程序中使用英制。
我如何构建我的表来应对差异,我正在考虑将所有测量值存储为整数,并使用外键来存储这种重量。
理想情况下,我希望能够在人们之间分享食谱并以他们喜欢的方式显示测量结果。
这是正确的方式
IngredientID PK
Weight int
TypeOfWeight int e.g. tsp=1,tbl=2,kilogram=3,pound=4,litre=5,ounce=6 etc
UserID int
还是偏离了轨道?任何建议都会很棒!
I am playing around with learning MVC and want to create a recipe recorder application to store my recipes.
I am using .net with Sql Server 2008 R2 however I don't think that really matters with what I am trying to do.
I want to be able to record all of the measures I use. In my country we use metric however I want people to be able to use imperial with my application.
How do I structure my table to cope with the differences, I was thinking of storing all of the measurements as ints and have a foreign key to store the kind of weight.
Ideally I would like to be able to share the recipes between people and display the measurements in their preferred way.
Is this the right kind of way
IngredientID PK
Weight int
TypeOfWeight int e.g. tsp=1,tbl=2,kilogram=3,pound=4,litre=5,ounce=6 etc
UserID int
Or is this way off track? Any suggestions would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该将重量(公斤/磅)等存储为单一重量类型(公制),并使用用户的偏好以正确的转换方式简单地“显示”它们。如果用户将重量设置设置为英制,则输入系统的值也需要进行转换。无论如何,这应该会简化您的数据。
与日期类似,您可以存储每个日期及其所在时区,或者以其他方式将所有日期存储为相同的(或没有时区),然后根据用户的偏好使用偏移量在应用程序中显示它们
I think you should store the weights (Kilo/Pound) etc as a single weight type (metric) and simply "display" them in the correct conversion using the user's preference. If the user has there weight settings set to Imperial, values entered into the system would need to be converted as well. This should simplify your data anyway.
Similar to Dates, you could store every date and what timezone it is from, or otherwise store all dates as the same (or no timezone) and then display them in the application using offsets according to the user's preference
如果您要存储权重(非离散值),我强烈建议使用数字或小数来存储该数据。您对
typeofweight
列的想法是正确的。在某处存储一个参考表,显示每个值的转换率(按照特定标准)。当您想要将盎司显示为 TSP 时,这会变得非常棘手,因为转换取决于成分本身,因此您需要第三个表 - 成分:id、名称、体积重量比。
typeofweight 表示例,其中
标准
单位为克成分体积到重量转换示例
因此,要显示
tsp
中的 500 盎司糖,您可以使用公式另一个具有 2 个重量的示例
或者
If you are storing weights (a non-discrete value) I would strongly suggest using numeric or decimal for this data. You have the right idea with the
typeofweight
column. Store a reference table somewhere showing what the conversion ratio is for each (to a certain standard).This gets quite tricky when you want to show ounces as TSP, because the conversion depends on the ingredient itself, so you need a 3rd table - ingredient: id, name, volume-to-weight ratio.
Example typeofweight table, where the
standard
unit is gramsExample ingredient volume to weight conversion
So to display 500 ounces of sugar in
tsp
, you would use the formulaAnother example with 2 weights
or