c# - problems with data contexts and disposing -


i have c# code looks like

using (datacontext db = new datacontext(program.config.dbcontextstr)) {     foo.bar(db); } 

so bar static method of class foo, , bar uses db object passed in. passes db object other methods calls.

the problem i'm getting exception:

system.objectdisposedexception: cannot access disposed object.  object name: 'datacontext accessed after dispose.'. 

i've looked around solutions , people have suggested forget using declaration , write:

datacontext db = new datacontext(blah); foo.bar(db);  // let garbage collector go merry business. 

and disable deferred loading:

db.deferredloadingenabled = false; foo.bar(db); 

i've tried both of these solutions, still exception. there other things should try?

you disposing data context.

firstly, way using data context correct, wrapping in using.

this means somewhere inside foo.bar, disposing data context; there no other alternative.

this means have search code either 1 of following constructs:

  • db.dispose(); or

  • using (db) { ... }.

try "find all" in visual studio on word "dispose" or "using" , manually check instances.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -