2011-05-26 43 views
5

我正在开发ASP.NET MVC3应用程序,并且我在MySQL 5.5中创建了一个数据库,其中包含一个公司表与联系人表具有一对多的关系。C#实体框架:已经有一个与此连接关联的开放DataReader,必须先关闭

Bedrijf(带导航属性“接触”)

联系

因为我不得不接管从我生成一个实体模型当前运行的网站上的这个数据库基础上该数据库和我写了以下代码来显示公司列表(按状态分组),提及该公司中的联系人数量:

个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> 

在我公司的名单,我想显示的号码分配给该公司的联系人,但我得到以下错误:

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

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

如何在启用延迟加载的情况下使用此工作?我的初学者ASP.NET(MVC)技能目前不能解决问题。

添加MultipleActiveResultSets = True;在web.config中connectionstring经常被指出为解决方案,但在我的情况下没有任何区别。

尝试了。在我的CompanyRespository中包含,同时将延迟加载设置为False,但我认为我没有做到这一点,因为我不熟悉他的语法。

这个描述也有道理;

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.

但不知道如何解决这个问题在我的代码与这些信息。在哪里/如何使用@ item.contacts.Count来显示联系人的数量?

在此先感谢。

回答

3

尝试使用这样的:

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

我认为MySQL连接可能不支持多个活动结果集,也因为在连接字符串中的设置对您没有帮助。

+0

奇怪了,我想,之前和它没有工作,现在它确实。 :) 谢谢! – tortuga 2011-05-26 15:05:07

14

我有类似的问题。注意到这是使用IEnumerable集合并调用另一个函数,查询数据库的位置。由于它是IEnumerable集合,读者是开放的。将IEnumerable更改为列表以解决问题。

+2

我完成了@AbbsHere在我的版本的这个问题上的建议,方法是在我的语句结尾添加一个.ToList()。 – badMonkey 2012-01-23 22:33:07

+0

我也是,谢谢你的提示! – Luke 2014-10-05 12:56:23

3

MySQL连接器不支持MultipleActiveResultSets,因此连接字符串修改将不起作用。

为了解决这个问题,在你的控制器,只需添加.ToList()方法,无论您的数据进行查询.. 例如...

public ActionResult Create() 
{ 
    ViewBag.PossibleCompanies = context.Companies.ToList(); 
    return View(); 
} 
相关问题