asp.net - Is it wise to dispose of my DBContext object before page render? -


i using dbcontext extensivly in asp.net web site project. typically have instance of dbcontext available page class , instantiate on page load , use throughout code, not disposing of however. wise me use pre_render event on page dispose of dbcontext (i assume done being used @ point?)

thanks tips.

solution

so ended writing little extension class dotnetnuke modules handles dbcontext me, seems work good, have rolled out in few modules far.

public class moduleext : portalmodulebase {     private bpentities _ent;     public bpentities ent      {         get{             if (_ent == null)             {                 _ent = new bpentities();             }             return _ent;         }     }     protected void page_prerender(object sender, eventargs e)     {         ent.dispose();     } } 

this enable me go ahead use ent object (my dbcontext) , not worry disposing done me on pre render.

you may that, if wish, you'll need keep 1 thing in mind: if ever, @ point, want reference child object/collection object you'll need make sure it's hydrated before dispose of context. otherwise, exception telling context has been disposed already.

some people have single context through application, , valid approach well, prefer you're doing right now. don't having database connections open of time.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -