title: entityframework There is already an open DataReader associated with this Command which must be closed first
body: <p>I have the following code that retrieves data from a customer table</p>
  
  <pre><code>var customers= context.CustomerEntities.Include("Addresses").Select(Mapper.Map).ToList();
  </code></pre>
  
  <p>The mapper function, maps the entity object, to a business object, and it looks like this</p>
  
  <pre><code>    internal static Customer Map(CustomerEntity entity)
      {
          if (entity == null)
              return null;
  
          return new Customer
          {
              Id = entity.Id,
              Name = entity.Name,
              Addresses = Map(entity.Addresses)
  
          };
      }
  </code></pre>
  
  <p>Now, the above code runs well. </p>
  
  <p>However, when I try to do this:</p>
  
  <pre><code>var customers= context.CustomerEntities.Select(Mapper.Map).ToList();
  </code></pre>
  
  <p>I get the error message: <code>There is already an open DataReader associated with this Command which must be closed first</code> when the Mapper function is being executed.</p>
  
  <p>Now I'm aware that to solve this problem, I have to set <code>multipleactiveresultsets=True</code> in my connection string. I have tried it, and it did solve my problem.</p>
  
  <p>However, when I ran SQL profiler, querying all customers from entity framework automatically retrieved all the addresses as well, even though I didn't need them.</p>
  
  <p>Is there a workaround besides having to set <code>multipleactiveresultsets=True</code>? I don't want the addresses to be lazy loaded all the time.</p>