c# - How to properly use a LINQ2SQL DataContext object; creating anew each use causes headaches -


so, in learning linq2sql (don't tell me it's dead technology please, i'm not switching ef simple applications) have read numerous times datacontext class meant created , destroyed frequently. great, lightweight object can use access database. however, in practice seems cause nothing trouble. example, if load datagrid changes entity objects don't seem tracked , calling datacontext.submitchanges has no effect:

void loadui() {     using( var db = new testdatacontext() )     {         // use dataloadoptions.loadwith<t> if need          // access foreign key/ deferred objects.         mastergrid.itemssource = db.customers.tolist();         detailsgrid.itemssource = // set collection of settings                                   // selected user in mastergrid.                                   // simple foreign key relationship     } }  void updatename( string newname ) {     using( var db = new testdatacontext() )     {         var customer = ((customer)mastergrid.selecteditem);         customer.name = newname;         db.submitchanges() // !!! database not updated !!!         } } 

so gives? creating new datacontext object when accessing db seems accepted/preferred practice, yet doesn't seem work in trivial of cases. appears object tracking doesn't work in scenario, how 1 possibly use datacontext class in practice? missing here? (note: hope missing simple.

i have assume case, don't know answer yet. keeping single dc around life of app, know not idea). in advance advice can offer.

btw, if of know of comprehensive article/serie4s of articles describe ins-and-outs of datacontext class love read it. haven't found examples go beyond trivial updates/inserts/deletes.

changed objects can updated datacontext created. datacontext typically created once every unit of work (e.g. page or form gets objects, things , updates them in db).

edit 2, in reaction foowombat's comment : more real-world example, read datacontext lifetime management on rick strahl's blog. goes in depth how implement handling datacontext happens behind scenes , can select/update/delete linq2sql objects without thinking (much) it.

edit 1 : also, if google around bit, find method called attach. method meant used objects have been deserialized , serialized back. mentions not try attach objects datacontext. however, in blogpost have example (ab)use attach can this. use if in pinch , need transfer objects 1 datacontext another. it's not recommended practice.

from blogpost :

public partial class employee {    public void detach()    {        this.propertychanged = null; this.propertychanging = null;        // assuming there's foreign key employee boss        this.boss = default(entityref<boss>);        // set child objects default        this.subordinates = default(entityset<subordinate>);    } } 

you can use detach method :

public static void updateemployee(employee employee) {     using (hrdatacontext datacontext = new hrdatacontext())     {         //attach datacontext         employee.detach();         datacontext.employees.attach(employee);         //save changes         datacontext.submitchanges();     } } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -