title: EF 4.1 Code First : How to load entity references into memory?
body: <p>I just started working with EF 4.1 Code First and noticed that by default, references (navigation properties), are not loaded into memory with a POCO entity you queried with LINQ-to-Entity. I have had no success with making the referenced entities load using <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k%28%22SYSTEM.DATA.ENTITY.INFRASTRUCTURE.DBENTITYENTRY%601.REFERENCE%22%29;k%28REFERENCE%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION=V4.0%22%29;k%28DevLang-CSHARP%29&amp;rd=true" rel="nofollow">DbEntityEntry.Reference</a>. When I call DbReferenceEntry.Load, the following exception is thrown:</p>
  
  <blockquote>
    <p>"There is already an open DataReader associated with this Command which must be closed first."</p>
  </blockquote>
  
  <p>Closing a DataReader is not something I really want to have to do when I'm in the middle of several LINQ queries. </p>
  
  <p>For example, the following will not work:</p>
  
  <pre><code>using (db1 = new NorthindDbContext(new SqlConnection(this.NORTHWIND))) { 
  orders = db1.Orders.Where(o =&gt; !(o.CustomerId == null || o.ShipperId == null || o.EmployeeID == null));
              foreach (var o in orders) {
                  Shipper s = o.Shipper;//exception: "There is already an open DataReader associated with this Command which must be closed first."
                  DbEntityEntry&lt;Order&gt; entry = db1.Entry(o);
                  DbReferenceEntry&lt;Order, Shipper&gt; shipper_reference = entry.Reference&lt;Shipper&gt;("Shipper");
                  if (!shipper_reference.IsLoaded) {
                      shipper_reference.Load();                   
                  }
              }
          }
  </code></pre>
  
  <p>Here is the Order class:</p>
  
  <pre><code>public partial class Order
  {
      public System.Int32 ID { get; set; }                
      public System.Nullable&lt;System.DateTime&gt; OrderDate { get; set; }
      public System.Nullable&lt;System.DateTime&gt; RequiredDate { get; set; }
      public System.Nullable&lt;System.DateTime&gt; ShippedDate { get; set; }       
      public System.Nullable&lt;System.Decimal&gt; Freight { get; set; }        
      public Employee Employee { get; set; }
      public Int32 CustomerId { get; set; }
      public Customer Customer { get; set; }
      public Int32 EmployeeID { get; set; }
      /// &lt;summary&gt;
      /// marked virtual for lazy loading
      /// &lt;/summary&gt;
      public virtual Shipper Shipper { get; set; }
      public Int32 ShipperId { get; set; }
  } 
  </code></pre>
  
  <p>I have tried marking the Order.Shipper property as virtual, and I still get the same exception if when I run the code.</p>
  
  <p>The <a href="http://msdn.microsoft.com/en-us/library/bb738708.aspx" rel="nofollow">ObjectQuery.Include</a> method, <em>does</em> work:</p>
  
  <pre><code>[TestMethod]    
  //configure MARS here?
  //Order.Shipper is not marked virtual now
  //...
              using (db = new NorthindDbContext(new SqlConnection(this.NORTHWIND))) {                         
              db.Orders.Include(o =&gt; o.Shipper)
  .Where(o =&gt; !(o.CustomerId == null || o.ShipperId == null || o.EmployeeID == null));
              foreach (var o in orders) {
                  Shipper s = o.Shipper;//null
                  DbEntityEntry&lt;Order&gt; entry = db.Entry(o);
                  DbReferenceEntry&lt;Order, Shipper&gt; shipper_reference = entry.Reference&lt;Shipper&gt;("Shipper");
                  if (!shipper_reference.IsLoaded) {
                      shipper_reference.Load();//"There is already an open DataReader associated with this Command which must be closed first."
                  }
  
  
              }
  </code></pre>
  
  <p>With EF 4.1 Code First, how do you make a referenced entity load into memory? </p>