As example I have following entities (many-to-many, I also removed unnessecary props):
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;}
}
Join entity:
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;}
}
I want to get all recipes that belong to a specific buffet and want include the recipe category.
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();
}
The list I get always returns Recipes with prop Category = null. I didn't find a solution to make the Include() work together with the Select()...
What am I doing wrong??
UPDATE:
I can make it work this way... but my feeling says this is not a good way because i have 2 ToList() calls... but now I have category included in my results:
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();
}