Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5942949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:19:31+00:00 2026-05-22T16:19:31+00:00

I’m working on a ASP.NET MVC3 application and I’ve created a database in MySQL

  • 0

I’m working on a ASP.NET MVC3 application and I’ve created a database in MySQL 5.5 which contains a company table having a one-to-many relationship with a contacts table.

table Bedrijf (with navigation property “contacts“)

table Contact

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:

CompanyRepository.cs

...

public IQueryable<Bedrijf> getCompaniesByStatus(int status)
    {
        return entities.Bedrijven.Where(c => c.bedrijf_status == status).OrderBy(c => c.bedrijf_naam);
    }

...

View calling 3 partial Views

@{Html.RenderPartial("ucCompaniesByStatus", Model.newCompanies, (new ViewDataDictionary { { "Titel", "Nieuwe bedrijven" } }));}

<br />

@{Html.RenderPartial("ucCompaniesByStatus", Model.activeCompanies, (new ViewDataDictionary { { "Titel", "Actieve bedrijven" } }));}

<br />

@{Html.RenderPartial("ucCompaniesByStatus", Model.inActiveCompanies, (new ViewDataDictionary { { "Titel", "Niet actieve bedrijven" } }));}

Partial View

@model IEnumerable<xxx.Models.Bedrijf>

<table id="companytable">
    <tr>
        <th id="thtitle">
            @ViewData["Titel"]
        </th>
        <th id="thactions"></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.ActionLink(@item.bedrijf_naam, "CompanyDetails", new { id = item.bedrijf_id }) 
            (@item.contacts.Count contact(en))



        </td>
        <td id="actions">
            @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 })
        </td>
    </tr>
}
</table>

In my list of companies, I would like to display the number of contacts assigned to that company but I got the following Error:

There is already an open DataReader associated with this Connection which must be closed first.

When go to my .edmx file and set Lazy Loading Enabled : False 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.):

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.

Adding MultipleActiveResultSets=True; in the web.config connectionstring is pointed out as solution often, but no difference in my case.

Tried the .Include 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.

This description makes also sense;

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.

but no idea how I should fix this problem in my code with this information. Where/How use @item.contacts.Count to show the number of contacts?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:19:32+00:00Added an answer on May 22, 2026 at 4:19 pm

    Try using this:

    public IQueryable<Bedrijf> getCompaniesByStatus(int status)
    {
        return entities.Bedrijven
                       .Include("contacts")
                       .Where(c => c.bedrijf_status == status)
                       .OrderBy(c => c.bedrijf_naam);
    }
    

    I think MySql connector probably doesn’t support multiple active result sets and because of that the setting in connection string didn’t help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.