none
Walking backwards after context disposed? RRS feed

  • Question

  • Hi all,

    I've got a data model where a object is categorized in a taxonomy.  As in, object "Foo" can be under sub-categories "A-1", "A-2", and "A-4" under category "A".  "A" also has sub-categories "A-3" and "A-5".  Using DataLoadOptions, I load my "Foo" object with the sub-category and category.  I now want a collection of the categories and sub-categories that relate to Foo after the data context has been disposed of.  I cannot figure out how to get this collection with getting an error about objects (I assume "A-3" and "A-5") being accessed after the data context was disposed.

    Any ideas?

    Thanks!
    Thursday, March 4, 2010 4:23 PM

Answers

  • I believe that the problem you're experiencing is due to the lazy loding of Linq. Especially if you enclose your use of the DataContext object within a 'using' block to dispose of it after its use, you're going to run into this issue. The way I handle it is to add a call to '.ToList()' at the end of my linq query. This grabs the data immediately so you won't run into the problem of the DataContext already being disposed before the query executes.
    Thursday, March 4, 2010 11:46 PM