C # Entity Framework: DataReader associated with this connection is already open, which should be closed first

I am working on an ASP.NET MVC3 application, and I created a database in MySQL 5.5 that contains a company table having a one-to-many relationship with a contact table.

Bedrijf table (with the navigation property " contacts ")

Contact table

Since I had to take this database from the current site, I created an Entity Model based on this database, and I wrote the following code to display a list of companies (grouped by status), indicating the number of contacts in this company:

CompanyRepository.cs

...

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

...

Call View 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>

, , :

DataReader, , .

.edmx Lazy Loading Enabled: False. ( ( 0), , .):

Lazy Loading Enabled? ASP.NET(MVC) .

MultipleActiveResultSets = True; web.config connectionstring , .

.Include CompanyRespository, False, , , .

:

. EF . , , ( ), DataReader . - DataReaders = MultipleActiveResultSets. (IQueryable), .

, . / @item.contacts.Count, ?

.

+5
3

:

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

, MySql, , , - .

+3

. , IEnumerable , . IEnumerable, . IEnumerable .

+14

The MySQL connector does not support MultipleActiveResultSets, so changing the connection string will not work.

To work around this problem, in your controller, simply add the .ToList () method to where your data is requested. For instance...

public ActionResult Create()
{
    ViewBag.PossibleCompanies = context.Companies.ToList();
    return View();
} 
+3
source

All Articles