2015-11-04 234 views
0

當我收到此錯誤錯誤運行LINQ查詢

"{"There is already an open DataReader associated with this Command which must be closed first."}" 

下面是關於它的錯誤代碼。

var summaryViewModel = new YogaSpaceSummaryViewModel 
      { 
       SpaceImage = (from u in space.Images orderby u.Ordering ascending select u.ImageThumbnail).First(), 
       Title = space.Overview.Title, 
       SpaceId = space.YogaSpaceId, 
       DateCreated = space.DateCreated.ToShortDateString(), 
       StepsToList = space.StepsToList, 
       Status = space.Status 
      }; 

這段代碼的哪一部分這裏

YogaSpaceOverviewViewModel overviewViewModel = new YogaSpaceOverviewViewModel(); 

     foreach (YogaSpace space in yogaSpaces) 
     { 
      var summaryViewModel = new YogaSpaceSummaryViewModel 
      { 
       SpaceImage = (from u in space.Images orderby u.Ordering ascending select u.ImageThumbnail).First(), 
       Title = space.Overview.Title, 
       SpaceId = space.YogaSpaceId, 
       DateCreated = space.DateCreated.ToShortDateString(), 
       StepsToList = space.StepsToList, 
       Status = space.Status 
      }; 

      overviewViewModel.YogaSpaceSummarys.Add(summaryViewModel); 
     } 
+0

正如消息所示,請檢查您的任何以前打開/使用過的DataReader是否已正確關閉。 – MusicLovingIndianGirl

+0

我該如何檢查? – user1186050

+0

發佈DB連接的代碼 – MusicLovingIndianGirl

回答

0
<connectionStrings> 
<add name="DatabaseEntities" connectionString="metadata=res://*/Database.csdl|res://*/Database.ssdl|res://*/Database.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=Server;Initial Catalog=Database;Persist Security Info=True;User ID=sa;Password=passowrd;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
</connectionStrings> 

啓用Webconfig

MultipleActiveResultSets
1

您可能需要使用使用這樣的範圍將導致所有對象向下包裹bedisposed。此外,爲了規避處置datacontext的lzy加載問題,您將會更好地服務於ToList()FirstOrDefault()您返回的任何結果,以確保集合已被枚舉,並且如果上下文被處置,則所有內容都是花哨的。

0

將MultipleActiveResultSets = true添加到連接字符串的提供者部分(其中指定了數據源,初始目錄等)。