Reputation: 652
I'm trying to eager load in related data DrinkCategory
. However, it is not working and I can't figure out why DrinkCategory is still null.
This is my Drink
model:
public class Drink
{
[Key]
public int DrinkId { get; set; }
public int DrinkCategoryId { get; set; }
public DrinkCategory DrinkCategory{ get; set; }
}
This is my DrinkCategory
model:
public class MenuItemViewModel
{
[Key]
public int DrinkCategoryId { get; set; }
public List<Drink> Drinks{ get; set; }
}
I eager load it like this:
_Context.Drinks.Include(item=> item.DrinkCategory);
Upvotes: 1
Views: 347
Reputation: 32069
You are missing .ToList
in your query. Write your query as follows:
_Context.Drinks.Include(item=> item.DrinkCategory).ToList();
Upvotes: 1