错误:ObjectContext 实例已被释放,不能再用于需要连接的操作

发布于 2024-11-17 09:36:27 字数 1672 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-11-24 09:36:27

在获取数据之前,您将关闭名为 contextCmsConnectionStringEntityDataModel。请记住,AsEnumerable() 将返回一个惰性枚举器,当您返回并处置 `context.

使用 ToArray() (或 ToList())确保在关闭源之前将数据加载到内存中。

You're closing the CmsConnectionStringEntityDataModel called context before getting the data. Remember that AsEnumerable() will return a lazy enumerator, no data has been read from the source when you return and dispose of `context.

Use ToArray() (or ToList()) to ensure you get the data into memory before closing the source.

此岸叶落 2024-11-24 09:36:27

调用 AsEnumerable 不会枚举集合,它只是将其从 IQeuryable 更改为 IEnumerble。您将在从数据库获取数据之前处理 ObjectContext。使用 ToList() 它将枚举集合并获取数据。

Calling AsEnumerable does not enumerate the collection it just changes it from IQeuryable<T> to IEnumerble<T>. You are disposing of the ObjectContext before the data is fetched from the database. Use ToList() which will enumerate the collection and fetch the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文