You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

采用Entity Framework Code-First如何建立一对多关系?菜谱应用代码求指导

菜谱应用EF Code-First设计方向确认与改进建议请求

我正在为配偶开发一款菜谱应用,希望在应用迭代过程中,她能直接向数据库添加新菜谱。添加新菜谱时,她可通过三个下拉菜单分别选择食材、计量单位和数量来构建菜谱配料。目前我已采用Entity Framework Code-First方式完成以下代码,想确认这个方向是否正确?创建表、控制器和视图后,菜谱配料索引视图的显示效果如图所示,恳请各位提供改进建议。

public class Recipes
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Image { get; set; }
}

public class UnitsModel
{
    [Key]
    public int Id { get; set; }
    public string UnitName { get; set; }
}

public class UnitQty
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

public class IngredientsModel
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

public class RecipeIngredients
{
    public int Id { get; set; }
    public int RecipesId  { get; set; }
    [ForeignKey("RecipesId")]
    public Recipes Recipes { get; set; }
    public int IngredientsModelId { get; set; }
    [ForeignKey("IngredientsModelId")]
    public IngredientsModel IngredientsModel { get; set; }  
    public int  UnitQtyId { get; set; }
    [ForeignKey("UnitQtyId")]
    public UnitQty UnitQty { get; set; }
    public int  UnitsModelId { get; set; }
    [ForeignKey("UnitsModelId")]
    public UnitsModel UnitsModel { get; set; }
}

菜谱配料索引视图

内容的提问来源于stack exchange,提问作者Marcus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:31:35