【发布时间】:2016-11-02 13:01:13
【问题描述】:
例如,我有以下实体(多对多,我还删除了不必要的道具):
public class Buffet
{
public int Id {get; set;}
public string Name {get; set;}
}
public class Recipe
{
public int Id {get; set;}
public string Name {get; set;}
public int CategoryId {get; set;}
public virtual Category Category {get; set;}
}
public class Category
{
public int Id {get; set;}
public string Name {get; set;}
}
加入实体:
public class BuffetRecipe
{
public int BuffetId {get; set;}
public virtual Buffet Buffet {get; set;}
public int RecipeId {get; set;}
public virtual Recipe Recipe {get; set;}
}
我想获取属于特定自助餐的所有食谱并希望包含食谱类别。
public IList<Recipe> GetRecipes(int buffetId)
{
return _dbContext.BuffetRecipes
.Where(item => item.BuffetId == buffetId)
.Include(item => item.Recipe)
.ThenInclude(item => item.Category)
.Select(item => item.Recipe)
.ToList();
}
我得到的列表总是返回带有 prop Category = null 的食谱。 我没有找到使 Include() 与 Select() 一起工作的解决方案...
我做错了什么??
更新:
我可以让它这样工作......但我感觉这不是一个好方法,因为我有 2 个 ToList() 调用......但现在我的结果中包含了类别:
public IList<Recipe> GetRecipes(int buffetId)
{
return _dbContext.BuffetRecipes
.Where(item => item.BuffetId == buffetId)
.Include(item => item.Recipe)
.ThenInclude(item => item.Category)
.ToList()
.Select(item => item.Recipe)
.ToList();
}
【问题讨论】:
-
你为什么要使用
BuffetRecipe类而不是Buffet和Recipe上的集合?你失去了在两者之间旅行的能力。您的BuffetRecipe类将被创建为您不需要在 C# 中管理多对多关系的表。 -
在 EF 核心中,没有连接实体就没有多对多...所以我必须手动创建一个 BuffetRecipe 实体以使这种关系在新的 EF 核心中工作...
-
我只想补充一点,包含在没有 Select 的情况下工作。这是设计使然吗?
-
Include仅在您不返回投影.Select(item => item.Recipe)时才有效。您应该以_dbContext.Recipes开始查询。 -
好的,感谢您提供的信息...您能帮我实现我想要的 linq 查询吗?
标签: entity-framework asp.net-core entity-framework-core .net-core