C#实体框架:已经有一个用此连接,必须先关闭相关联的打开的DataReader [英] C# Entity Framework: There is already an open DataReader associated with this Connection which must be closed first

查看:302
本文介绍了C#实体框架:已经有一个用此连接,必须先关闭相关联的打开的DataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC3应用程序,我创建了一个数据库中的的MySQL 5.5 其中包含一个公司的表具有联系人表一到一对多的关系。

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.

表中的 Bedrijf (带导航属性联系人

由于我不得不接管当前正在运行的网站,我产生了实体模型基于该数据库这个数据库,我写了下面code,以显示公司的名单(按状态分组),提数在联系人:

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);
    }

...

查看调用3部分景色

@{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" } }));}

管窥

@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:

有已与此连接,必须先关闭相关联的打开的DataReader。

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

在去我.edmx文件和设置的延迟加载启用:假我能够得到的结果(但在我接触的计数不工作(我得到0),假设我相关的联系人现在还没有加载):

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.):

如何我能得到这个工作与延迟加载启用?我初学ASP.NET(MVC)的技能不要把我此刻的解决方案。

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.

添加的 MultipleActiveResultSets = TRUE; 的在web.config中的ConnectionString指出作为解决方案的时候,但对我来说没有什么区别

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

试过在 .INCLUDE 在我CompanyRespository而有延迟加载设置为False,但我想我没有这样做,因为正确我不熟悉他witht语法。

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.

这说明使也感;

这是不是关闭连接。 EF
  正确地管理连接。我的
  这个问题的理解是
  有多个数据检索
  命令在单个连接执行
  (或多个单命令
  选择),而下一DataReader的是
  在执行之前第一个具有
  完成读出。只有这样,才能
  避免该异常是允许
  多重嵌套的DataReader =开启
  MultipleActiveResultSets。另一个
  当这总是会发生的情况是
  当您通过的结果迭代
  查询(IQueryable的),你会
  触发延迟加载加载实体
  迭代里面。

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.

但不知道我应该怎么在我的code。与此信息解决此问题。凡/如何使用 @ item.contacts.Count ,以显示联系人的号码吗?

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?

先谢谢了。

推荐答案

请尝试使用这样的:

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

我想的MySQL Connector可能不支持多活动结果集,也因为在连接字符串中的设置也帮不了你。

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.

这篇关于C#实体框架:已经有一个用此连接,必须先关闭相关联的打开的DataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆