【发布时间】: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 了解更多信息。您必须创建一个模型类来链接您的
DietList和Meal实例。
标签: c# sql database entity-framework asp.net-core