title: Entity Framework 6 Load List on Eager Load
body: <p>Entity Framework is loading everything in the <code>Polygon</code> but my list of <code>LineSegments</code>. What am I missing?</p>
  
  <p>Polygon:</p>
  
  <pre><code>public class Polygon
  {
      List&lt;LineSegment&gt; _lineSegments = new List&lt;LineSegment&gt;();
      public List&lt;LineSegment&gt; LineSegments
      {
          get { return _lineSegments; }
          protected set { _lineSegments = value; }
      }
  
      public Point BasePoint { get; protected set; }
  
      public Vector NormalVector { get; protected set; }
  
      public int? DatabaseId { get; set; }
  
      // ...
  }
  </code></pre>
  
  <p>LineSegment class (<code>Polygon</code> has a list of these)</p>
  
  <pre><code>public class LineSegment
  {
      public virtual Distance Magnitude
      {
          get { return _magnitude; }
          set { _magnitude = value; }
      }
      private Distance _magnitude;
  
      public virtual Point BasePoint
      {
          get { return _basePoint; }
          set { this._basePoint = value; }
      }
      private Point _basePoint;
  
      public virtual Direction Direction
      {
          get { return _direction; }
          set { _direction = value; }
      }
      private Direction _direction;
  
      public int? DatabaseId { get; set; }
  
      // ...
  }
  </code></pre>
  
  <p>And here is the relationship setup in the model:</p>
  
  <pre><code>modelBuilder.Entity&lt;Polygon&gt;()
          .HasMany(polygon =&gt; polygon.LineSegments);
  </code></pre>
  
  <p>So there is a table for both Polygons and LineSegments, and they insert properly where the LineSegment has a reference to a Polygon. But when I try to retrieve them using eager loading, it doesn't load the list. I have the properties of LineSegment listed in the includes, but it's not working. I think I need to amend the relationship setup in the model, but I'm not sure how. <strong>How can I correct this so that I load the list of LineSegments eagerly when I load a Polygon?</strong> Here's the querying:</p>
  
  <pre><code>    private static List&lt;Expression&lt;Func&lt;Polygon, object&gt;&gt;&gt; polygonRegionNaviationProperties = new List&lt;Expression&lt;Func&lt;Polygon, object&gt;&gt;&gt;()
      {
          (polygon =&gt; polygon.BasePoint),
          (polygon =&gt; polygon.BasePoint.X),
          (polygon =&gt; polygon.BasePoint.Y),
          (polygon =&gt; polygon.BasePoint.Z),
          (polygon =&gt; polygon.NormalVector),
          (polygon =&gt; polygon.NormalVector.Direction),
          (polygon =&gt; polygon.NormalVector.Direction.Phi),
          (polygon =&gt; polygon.NormalVector.Direction.Theta),
          (polygon =&gt; polygon.NormalVector.Magnitude),
          (polygon =&gt; polygon.NormalVector.BasePoint.X),
          (polygon =&gt; polygon.NormalVector.BasePoint.Y),
          (polygon =&gt; polygon.NormalVector.BasePoint.Z),
          (polygon =&gt; polygon.LineSegments),
          (polygon =&gt; polygon.LineSegments.Select(lineSegment =&gt; lineSegment.Direction)),
          (polygon =&gt; polygon.LineSegments.Select(lineSegment =&gt; lineSegment.Direction.Phi)),
          (polygon =&gt; polygon.LineSegments.Select(lineSegment =&gt; lineSegment.Direction.Theta)),
          (polygon =&gt; polygon.LineSegments.Select(lineSegment =&gt; lineSegment.Magnitude)),
          (polygon =&gt; polygon.LineSegments.Select(lineSegment =&gt; lineSegment.BasePoint.X)),
          (polygon =&gt; polygon.LineSegments.Select(lineSegment =&gt; lineSegment.BasePoint.Y)),
          (polygon =&gt; polygon.LineSegments.Select(lineSegment =&gt; lineSegment.BasePoint.Z))
      };
  
      public Polygon GetPolygon(int? databaseId)
      {
          if(databaseId != null)
          {
              Polygon retrievedPolygon = Query((polygon =&gt; polygon.DatabaseId == databaseId), polygonRegionNaviationProperties);
              return retrievedPolygon;
          }
          else
          {
              return null;
          }
      }
  
      public override Polygon Query(Expression&lt;Func&lt;Polygon, bool&gt;&gt; match, List&lt;Expression&lt;Func&lt;Polygon, object&gt;&gt;&gt; includes = null)
      {
          using (var databaseContext = new ClearspanDatabaseContext())
          {
              databaseContext.Database.Log = Console.Write;
  
              if (includes != null)
              {
                  var dataSet = databaseContext.Set&lt;Polygon&gt;(); // Get the relevant DataSet
                  Polygon retrievedObject = includes.Aggregate( // Eagerly load the passed navigation properties
                          dataSet.AsQueryable(),
                          (current, include) =&gt; current.Include(include)
                      ).SingleOrDefault(match);
                  databaseContext.Entry(retrievedObject).Collection(polygon =&gt; polygon.LineSegments).Load();
                  return retrievedObject;
              }
              else
              {
                  Polygon retrievedObject = databaseContext.Set&lt;Polygon&gt;().SingleOrDefault(match);
                  databaseContext.Entry(retrievedObject).Collection(polygon =&gt; polygon.LineSegments).Load();
                  return retrievedObject;
              }
          }
      }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is a link to a thinned out <a href="https://bitbucket.org/Clearspan/testlibrary" rel="nofollow">project</a> illustrating my problem. </p>
  
  <ol>
  <li>Clone it down.</li>
  <li>Initialize the submodules (<code>git submodule init</code> and <code>git submodule update</code> in the root of the project) (this may happen automatically if you use SourceTree)</li>
  <li>Run the PostgreSQL script, noting that you can't run it all at once since it's creating a database. Follow the comments at the top.</li>
  <li>Run the unit tests, and note that they all should pass except InsertAndRetrievePolygon. That's because the List isn't retrieved, which you can see by hovering over the variables in the debugger.</li>
  </ol>
  
  <p>Hopefully this is helpful to understanding, diagnosing, and solving this problem. You'll note that in my question I simplified the hierarchy that actually exists in the GeometryClassLibrary. Thanks, Scott H</p>