0

I am new to EF and I have the following query:

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;

Problem is when I load the page I get the following error:

"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."

Any help would be fantastic.

Error in detail:

AccountType = '((new System.Collections.Generic.Mscorlib_CollectionDebugView<Test.MemberAccount>
(userAccounts)).Items[0]).AccountType' 
threw an exception of type 'System.ObjectDisposedException'
Lord Relix
  • 922
  • 5
  • 23
  • 50
  • 1
    Are you sure that this is the code that is causing your exception? You are resolving your query to an in-memory collection with .ToList() so this code doesn't seem to suspect. – David L Jun 03 '15 at 00:55
  • Yes, Look: AccountType = '((new System.Collections.Generic.Mscorlib_CollectionDebugView(userAccounts)).Items[0]).AccountType' threw an exception of type 'System.ObjectDisposedException' – Lord Relix Jun 03 '15 at 00:58
  • 1
    It looks like you're accessing a navigational property after you've returned your list. Lazy loading is trying to use the invalid context. – Anthony Pegram Jun 03 '15 at 01:01
  • That may be it. How would I go about with fixing this issue? Or is it impossible and I need to look for an alternative? – Lord Relix Jun 03 '15 at 01:02

2 Answers2

3

AccountType is not being eagerly loaded. Since you're sticking to query syntax, it would look like the following:

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;

If intellisense is missing, make sure to include the following using statement:

using System.Data.Entity;
David L
  • 32,885
  • 8
  • 62
  • 93
  • Hmm, weird, after I close the parenthesis I get no option to type Include. – Lord Relix Jun 03 '15 at 01:07
  • Ah, nevermind had to use 'using System.Data.Entity;' – Lord Relix Jun 03 '15 at 01:09
  • 1
    @LordRelix, If it were me (and I haven't used EF6, just 4), I would apply the Include earlier => context.MemberAccount.Include(...), and you can omit the join since you're not using it in your query and EF would know what to do based on the relationship. – Anthony Pegram Jun 03 '15 at 01:09
  • Ah I see. Yes I added the Include earlier as you suggested and it works. Thanks a million, learned a few things =) – Lord Relix Jun 03 '15 at 01:12
2

You need to eagerly load AccountType for your MemberAccounts:

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;
JleruOHeP
  • 10,106
  • 3
  • 45
  • 71