【问题标题】:Multiple item in object对象中的多个项目
【发布时间】:2018-11-22 17:28:00
【问题描述】:

c# : EF 关系 .net 核心 下午好 我正在尝试为营养师创建服务。 我想让 Db 允许: 一种饮食可以多餐,但 一餐可以多种饮食 现在我有

public class DietList
    {
        public Guid DietListId { get; set; }
        [Display(Name = "Nazwa diety")]
        public string DietName { get; set; }

        public DateTime AddedDataTime { get; set; }
        public string Describe { get; set; }

        public List<Meal> MealId { get; set; }
    }

public class Meal
    {
        public Guid MealId { get; set; }
        [Display(Name = "Nazwa Posiłku")]
        public string MealName { get; set; }
        [Display(Name = "Typ Posiłku")]
        public string MealType { get; set; }
        [Display(Name = "Składniki")]
        public string Components { get; set; }

        public List<DietList>  DietListId { get; set; }
    }

但它抛出异常: 无法确定“List”类型的导航属性“DietList.MealId”表示的关系。 这个问题有解决办法吗?

【问题讨论】:

  • 是 dotnet core 还是 .Net Framework?
  • 没有足够的信息,但我认为您需要更改导航属性的名称。
  • 这是一个 .net 核心,我添加了相关信息
  • 是的,将您的名字从 MealId -> MealList 和 DietListId -> DietList 更改。这可能不是你的问题,但它更清楚。还有,有没有流畅的代码?
  • 如果您使用的是 Entity Framework Core,则无法在没有中间表的情况下对多对多关系进行建模。阅读these docs 了解更多信息。您必须创建一个模型类来链接您的 DietListMeal 实例。

标签: c# sql database entity-framework asp.net-core


【解决方案1】:

根据您的描述,DietMeal 之间存在多对多关系。 它需要一个额外的/中间表来存储实体的链接,比如DietMeals。 然后可以在DbContext类中指定关系。

您可以参考this article


属性命名看起来有点混乱。集合名称应为复数形式,例如:

Public List<Meal> Meals { get; set; }

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多