title: C# Entity Framework: There is already an open DataReader associated with this Connection which must be closed first
body: <p>I'm working on a ASP.NET MVC3 application and I've created a database in <strong>MySQL 5.5</strong> which contains a company table having a one-to-many relationship with a contacts table.</p>
  
  <p>table <strong>Bedrijf</strong> (with navigation property "<strong>contacts</strong>")</p>
  
  <p>table <strong>Contact</strong></p>
  
  <p>Since I had to take over this database from a currently running site I generated a Entity Model based on that database and I wrote the following code to display a list of companies (grouped by status), mentioning the number of contacts in that company:</p>
  
  <p><strong>CompanyRepository.cs</strong></p>
  
  <pre><code>...
  
  public IQueryable&lt;Bedrijf&gt; getCompaniesByStatus(int status)
      {
          return entities.Bedrijven.Where(c =&gt; c.bedrijf_status == status).OrderBy(c =&gt; c.bedrijf_naam);
      }
  
  ...
  </code></pre>
  
  <p><strong>View calling 3 partial Views</strong></p>
  
  <pre><code>@{Html.RenderPartial("ucCompaniesByStatus", Model.newCompanies, (new ViewDataDictionary { { "Titel", "Nieuwe bedrijven" } }));}
  
  &lt;br /&gt;
  
  @{Html.RenderPartial("ucCompaniesByStatus", Model.activeCompanies, (new ViewDataDictionary { { "Titel", "Actieve bedrijven" } }));}
  
  &lt;br /&gt;
  
  @{Html.RenderPartial("ucCompaniesByStatus", Model.inActiveCompanies, (new ViewDataDictionary { { "Titel", "Niet actieve bedrijven" } }));}
  </code></pre>
  
  <p><strong>Partial View</strong></p>
  
  <pre><code>@model IEnumerable&lt;xxx.Models.Bedrijf&gt;
  
  &lt;table id="companytable"&gt;
      &lt;tr&gt;
          &lt;th id="thtitle"&gt;
              @ViewData["Titel"]
          &lt;/th&gt;
          &lt;th id="thactions"&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model)
  {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.ActionLink(@item.bedrijf_naam, "CompanyDetails", new { id = item.bedrijf_id }) 
              (@item.contacts.Count contact(en))
  
  
  
          &lt;/td&gt;
          &lt;td id="actions"&gt;
              @Html.ActionLink("Edit", "CompanyEdit", new { id=item.bedrijf_id }) |
              @Html.ActionLink("Details", "CompanyDetails", new { id = item.bedrijf_id }) |
              @Html.ActionLink("Delete", "Delete", new { id = item.bedrijf_id })
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  &lt;/table&gt;
  </code></pre>
  
  <p>In my list of companies, I would like to display the number of contacts assigned to that company but I got the following Error: </p>
  
  <blockquote>
    <p>There is already an open DataReader associated with this Connection which must be closed first.</p>
  </blockquote>
  
  <p>When go to my .edmx file and set <strong>Lazy Loading Enabled : False</strong> I'm able to get a result (But the count on my contacts is not working (I get 0), assuming my related contacts are not loaded now.):</p>
  
  <p>How Can I get this working with Lazy Loading Enabled? My beginner ASP.NET (MVC) skills don't bring me to a solution at the moment.  </p>
  
  <p>Adding <em>MultipleActiveResultSets=True;</em> in the web.config connectionstring is pointed out as solution often, but no difference in my case.</p>
  
  <p>Tried the <strong>.Include</strong> in my CompanyRespository while having lazy loading set to False, but I think I didn't do that correctly since I'm not familiar witht he syntax.</p>
  
  <p>This description makes also sense;</p>
  
  <blockquote>
    <p>It is not about closing connection. EF
    manages connection correctly. My
    understanding of this problem is that
    there are multiple data retrieval
    commands executed on single connection
    (or single command with multiple
    selects) while next DataReader is
    executed before first one has
    completed the reading. The only way to
    avoid the exception is to allow
    multiple nested DataReaders = turn on
    MultipleActiveResultSets. Another
    scenario when this always happens is
    when you iterate through result of the
    query (IQueryable) and you will
    trigger lazy loading for loaded entity
    inside the iteration.</p>
  </blockquote>
  
  <p>but no idea how I should fix this problem in my code with this information. Where/How use <strong>@item.contacts.Count</strong> to show the number of contacts?</p>
  
  <p>Thanks in advance.</p>