我是EF新手,我有以下疑问:
List<Test.MemberAccount> userAccounts = new List<Test.MemberAccount>();
using (var context = new CoopEntities1())
{
var query = from s in context.MemberAccount
join sa in context.AccountType on s.account_type_id equals sa.id
where s.member_guid == memberID
select s;
userAccounts = query.ToList();
}
return userAccounts;
问题是当我加载页面时出现以下错误:
&#34; ObjectContext实例已被处理,不能再用于需要连接的操作。&#34;
任何帮助都会很棒。
详细错误:
AccountType = '((new System.Collections.Generic.Mscorlib_CollectionDebugView<Test.MemberAccount>
(userAccounts)).Items[0]).AccountType'
threw an exception of type 'System.ObjectDisposedException'
答案 0 :(得分:3)
没有急切地加载AccountType。由于您坚持使用查询语法,因此它将如下所示:
List<Test.MemberAccount> userAccounts = new List<Test.MemberAccount>();
using (var context = new CoopEntities1())
{
var query = (from s in context.MemberAccount
join sa in context.AccountType on s.account_type_id equals sa.id
where s.member_guid == memberID
select s).Include("AccountType");
userAccounts = query.ToList();
}
return userAccounts;
如果缺少intellisense,请确保包含以下using语句:
using System.Data.Entity;
答案 1 :(得分:2)
您的会员帐户需要eagerly load AccountType:
List<Test.MemberAccount> userAccounts = new List<Test.MemberAccount>();
using (var context = new CoopEntities1())
{
userAccounts = context.MemberAccount
.Where(m => m.member_guid == memberID)
.Include(m => m.AccountType)
.ToList();
}
return userAccounts;