2011-05-26 82 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(); 
} 
相關問題