错误:ObjectContext 实例已被释放,不能再用于需要连接的操作
我在 C# 中使用 asp.net 和 EF 4。
我有一个与ObjectDataSource
关联的DetailsView。
<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.
知道我做错了什么以及如何修复它吗?
I use asp.net and EF 4 in C#.
I have a DetailsView with associated a ObjectDataSource
.
<asp:ObjectDataSource ID="uxEntityDataSourceAuthors" runat="server"
SelectMethod="GetAuthors"
TypeName="WebProject.Web.Cms.AdminCms.Sections.CmsContents.ContentInformation">
</asp:ObjectDataSource>
This the code for the Method:
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;
}
When I'm running the code I receive this error:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Any idea what I'm doing wrong and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在获取数据之前,您将关闭名为
context
的CmsConnectionStringEntityDataModel
。请记住,AsEnumerable()
将返回一个惰性枚举器,当您返回并处置 `context.使用
ToArray()
(或ToList()
)确保在关闭源之前将数据加载到内存中。You're closing the
CmsConnectionStringEntityDataModel
calledcontext
before getting the data. Remember thatAsEnumerable()
will return a lazy enumerator, no data has been read from the source when you return and dispose of `context.Use
ToArray()
(orToList()
) to ensure you get the data into memory before closing the source.调用 AsEnumerable 不会枚举集合,它只是将其从
IQeuryable
更改为IEnumerble
。您将在从数据库获取数据之前处理 ObjectContext。使用ToList()
它将枚举集合并获取数据。Calling AsEnumerable does not enumerate the collection it just changes it from
IQeuryable<T>
toIEnumerble<T>
. You are disposing of the ObjectContext before the data is fetched from the database. UseToList()
which will enumerate the collection and fetch the data.