Gib*_*boK 2 c# linq-to-entities entity-framework objectdatasource entity-framework-4
我在C#中使用asp.net和EF 4.
我有一个关联的DetailsView ObjectDataSource.
<asp:ObjectDataSource ID="uxEntityDataSourceAuthors" runat="server"
SelectMethod="GetAuthors"
TypeName="WebProject.Web.Cms.AdminCms.Sections.CmsContents.ContentInformation">
</asp:ObjectDataSource>
Run Code Online (Sandbox Code Playgroud)
这个方法的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我收到此错误:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么以及如何解决它?
您在获取数据之前关闭了CmsConnectionStringEntityDataModel被调用者context.请记住,AsEnumerable()将返回一个惰性枚举器,当您返回并处理`context时,没有从源读取数据.
使用ToArray()(或ToList())确保在关闭源之前将数据存入内存.
| 归档时间: |
|
| 查看次数: |
6073 次 |
| 最近记录: |