title: Entity Framework 6, eager loading nested relationship returns empty collection
body: <p>I have a domain model that looks like this (stripped of some unimportant properties)</p>
  
  <pre><code>public class User
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public virtual int UserId { get; set; }
  
      private ICollection&lt;Recipe&gt; _recipes;
      public virtual ICollection&lt;Recipe&gt; Recipes
      {
          get { return _recipes ?? new List&lt;Recipe&gt;(); } 
          set { _recipes = value; }
      } 
  }
  
  public class Recipe
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int RecipeId { get; set; }
  
      private ICollection&lt;Ingredient&gt; _ingredients;
      public virtual ICollection&lt;Ingredient&gt; Ingredients
      {
          get { return _ingredients ?? new List&lt;Ingredient&gt;(); } 
          set { _ingredients = value; }
      }
      public User User { get; set; }
  }
  
  public class Ingredient
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int IngredientId { get; set; }
  
      public Recipe Recipe { get; set; }
  }
  </code></pre>
  
  <p>So in short, <code>User</code> has one-to-many relationship to <code>Recipe</code>, which in turn has one-to-many relationship to <code>Ingredient</code>. I'm using code first approach, with this mapping in <code>protected override void OnModelCreating(DbModelBuilder modelBuilder)</code> :</p>
  
  <pre><code>modelBuilder.Entity&lt;User&gt;()
      .HasMany(u =&gt; u.Recipes)
      .WithRequired(u =&gt; u.User)
      .WillCascadeOnDelete(true);
  
  modelBuilder.Entity&lt;Recipe&gt;()
      .HasMany(u =&gt; u.Ingredients)
      .WithRequired(u =&gt; u.Recipe)
      .WillCascadeOnDelete(true);
  </code></pre>
  
  <p>and I'm using repository, which has this code to store data into MySQL database:</p>
  
  <pre><code>public void Add(User entity)
  {
      using (var context = new UserContext())
      {
          context.Users.Add(entity);
          context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>and fetching:</p>
  
  <pre><code>public User GetById(int id)
  {
      using (var context = new UserContext())
      {
          var user = context.Users
              .Include(u =&gt; u.Recipes)
              .Include(u =&gt; u.Recipes.Select(x =&gt; x.Ingredients))
              .FirstOrDefault(u =&gt; u.UserId == id);
  
          return user;
      }
  }
  </code></pre>
  
  <p>I have integration tests, which create a <code>User</code> object, <code>List&lt;Recipe&gt;</code> with two recipes where each has <code>List&lt;Ingredient&gt;</code> with 2 items. They are different for each recipe, so 4 ingredients total. When I add it to the repository, I can see it in the database with correct PKs and FKs. </p>
  
  <p>However when retrieving from database, returned <code>User</code> object has recipes, but neither of those recipes has the ingredients in them. Due the the way I set up my getters, when I try to access, them, I receive an empty collection.</p>
  
  <p>e.g. doing this:</p>
  
  <pre><code>/* create recipes collection and seed it */
  
  User user = new User {Recipes = recipes /* plus some omitted properites*/};
  _repository.Add(user);
  
  var returnedUser = _repository.GetById(user.UserId);
  
  var userIngredients = user.Recipes.First(u =&gt; u.RecipeName == "Test").Ingredients.ToList();
  var returnedIngredients = returnedUser.Recipes.First(u =&gt; u.RecipeName == "Test").Ingredients.ToList();
  </code></pre>
  
  <p><code>returnedIngredients</code> is empty, while <code>userIngredients</code> has two elements. This then fails assertion and results in failed integration test.</p>
  
  <p>Can someone tell me how to properly do eager loading on nested one-to-many?</p>