我在C#中使用asp.net和EF 4。錯誤:ObjectContext實例已被處置,不能再用於需要連接的操作。
我有一個DetailsView與關聯的ObjectDataSource
。
<asp:ObjectDataSource ID="uxEntityDataSourceAuthors" runat="server"
SelectMethod="GetAuthors"
TypeName="WebProject.Web.Cms.AdminCms.Sections.CmsContents.ContentInformation">
</asp:ObjectDataSource>
這對於方法的代碼:
public IEnumerable<CmsAuthor> GetAuthors()
{
if (Roles.IsUserInRole("CMS-AUTHOR"))
{
using (CmsConnectionStringEntityDataModel context = new CmsConnectionStringEntityDataModel())
{
// Display all Authors for specific logged-in User.
// Get Guid for current logged-in User.
MembershipUser myLoggedinUser = Membership.GetUser();
Guid myUserGuid = (Guid)myLoggedinUser.ProviderUserKey;
// Get list of Authors filtering my current logged-in User.
var myAuthorsForAuthor = from a in context.CmsAuthors
where a.UserId == myUserGuid
orderby a.LastName ascending
select a;
return myAuthorsForAuthor.AsEnumerable();
}
}
return null;
}
當我運行的代碼,我收到此錯誤:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
任何想法,我做錯了,如何解決?
列表和數組方法分別稱爲ToList()和ToArray()分別爲 –
@BenRobinson:我不能相信我犯了這個錯誤:-)。謝謝。 – Richard
嗨,Id沒有AsArray()(或AsList())只有ToList()。 – GibboK