0

There are two tables: Account (id) and BranchId(Id,Name,AccountId) and I have this code in pageload

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       using (var db = new SitesDataContext())
       {
           BranchGrid.DataSource = db.Branches.Where(b=>b.AccountId == loggedInUser.AccountId).ToList();
                    BranchGrid.DataBind();
       }
    }            
}

I understand about Linq and lazy loading and that is why I used ToList() But I was still getting error.

Now if I remove the association b/w Branch and Account, the above code works fine. However it throws exception with the association.

How can this be resolved while still keeping the association?

Thanks

Jags
  • 1,466
  • 1
  • 18
  • 33

2 Answers2

3

Even with the ToList() your Accounts are not eagerly loaded, only the Branches

You can either:

  • remove the using {} and let the garbage collector do its job

or

  • add LoadOptions to the datacontext to eager load the Accounts with the branches (something like this)

     DataLoadOptions options = new DataLoadOptions();
     options.LoadWith<Branches>(b => b.Account);
     Context.LoadOptions = options;
    
Pleun
  • 8,856
  • 2
  • 30
  • 50
  • Don't remove using{} - DataContext implements IDisposable, and must be Disposed at some point. The garbage collector does not do this for you. – Amy B Oct 16 '11 at 11:51
  • DataContext holds state (for example, a SqlConnection and pointers to the objects you have retrieved). These will eventually get cleaned up by the GC once you release all references, but some of these objects (for example, the underlying SqlConnection) may be holding resources that you typically want to release as soon as your are finished, rather than relying on the GC to clean up. http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2625b105-2cff-45ad-ba29-abdd763f74fe/ – Pleun Oct 16 '11 at 12:02
  • @DavidB or http://leedumond.com/blog/about-disposing-the-datacontext/ and finally http://stackoverflow.com/questions/389822/when-should-i-dispose-of-a-data-context – Pleun Oct 16 '11 at 12:04
  • thats the thing..I dont want Account to load at all..not sure why they are being fetched! – Jags Oct 16 '11 at 13:49
  • that something we cannot see from this part of the code. You need to post the rest in that case. – Pleun Oct 17 '11 at 08:20
  • @Pleun : I've read the internet opinions you linked. Then I read msdn on IDisposable. The garbage collector does not call Dispose for you. If you would like Dispose called, you must do it yourself. "using" is the most straightforward way to accomplish this. If you "remove the using {} and let the garbage collector do its job", then Dispose will never be called. – Amy B Oct 17 '11 at 12:45
  • So interesting to see what is the 'truth'... Worth a separate question? – Pleun Oct 17 '11 at 12:59
0

Somewhere, the Account property of a Branch instance is being accessed. You must locate the code that is accessing that property and stop it.

Amy B
  • 108,202
  • 21
  • 135
  • 185
  • looked at the call stack. all it shows is external code. and also this is completely new code which I just added and theres no where else where I am directly using. I am binding the result to jQGrid. and theres no where I am specifying those columns to be binded. – Jags Oct 16 '11 at 13:48
  • The external code must be serializing your instances out to the browser. LinqToSql query modeling objects are meant to be used to make queries, not to be pushed across the wire. – Amy B Oct 17 '11 at 12:49