Geeks With Blogs

Tim Huffam Dotting the I and crossing the T of I.T.
Exception: System.ObjectDisposedException: Cannot access a disposed object. 
Object name: 'DataContext accessed after Dispose.'.
 
This can occur when using Linq to SQL to retrieve data from a database - and when you try to access data after the data context object has been disposed.  More specifically, the exception occurs when trying to access an item that has not yet been retrieved from the database and the data context object has been disposed. 
 
This often occurs when using a variable, that was populated within a using(...datacontext) block, after the using() block.  This works fine when accessing data explicitly retrieved within the using() block - but the exception occurs when you try to access other items that were not retrieved eg a related item such as a foreign key or child table.
 
To reproduce this error, comment out lines 6 and 9 in the sample code below.  This will raise the exception on line 11.
We can be solve this by telling the data context to explicitly retrieve the items you'll need.  This can be done a number of ways while the datacontext is still in scope.
  1. Access the items directly eg with a linq expression eg something followed by .ToList(), .Single() or .First().  Line 9 does this in the sample below.
  2. Use the DataLoadOptions class  - this is a way of telling the datacontext what additional data to retrieve.  Lines 4, 5 and 6 do this in the sample below (6 is commented out just while testing line 9).
* Note that you only need one of these - so either include lines 4-6 (all uncommented) or line 9.
 
The following code shows how to do both.  This is based on a database using a table called Parent and a table called Child.  The Child table has a ParentId field that is a foreign key to the Parent table.  The DataClasses1DataContext was generated using Linq to SQL.

1.  Parent outerParent;
2.  using (var db = new DataClasses1DataContext
())
3.  {
4.   DataLoadOptions dlo = new DataLoadOptions
();
5.   dlo.LoadWith<Parent
>(p => p.Childs);
6.  
//db.LoadOptions = dlo;
7.  
var outerParent = (from parent in
db.Parents
8.                     select
parent).First();
9.   List<Child
> children = outerParent.Childs.ToList(); 
10. }
11. int i = outerParent.Childs.Count;

HTH
Tim
Posted on Tuesday, June 3, 2008 9:41 AM C# .NET , SQL Server , ASP.NET | Back to top


Comments on this post: System.ObjectDisposedException: Cannot access a disposed object. Getting Linq to prepopulate/explicitly fetch data from the database.

# re: System.ObjectDisposedException: Cannot access a disposed object. Getting Linq to prepopulate/explicitly fetch data from the
Requesting Gravatar...
Using the DataLoadOptions is an interesting idea. I had previously used the ToList() to avoid this problem. Thanks for showing a new way to do this.
Left by Philip on Jun 03, 2008 11:33 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net