c# - Keep DbContext open for DataGridView -


i have datagridview , datagridview's datasource bindinglist got entity framework (v6) via context.person.local.tobindinglist(). after set datasource bindinglist, dispose context, because read keeping context open bad practice.

so, if wanted add new row, click on "add" button comes bindingnavigator got created when dragged "people" object data source windows form. every time click "add" button, exception tells me context has been disposed. need keep context open time when using datagridview? oh and: datasource might change during runtime depending on selection of listbox item.

also, when context has been disposed , edited 1 row datagridview, how find out (after multiple changes) row has changed? tried do:

foreach(datagridviewrow row in peopledatagridview.rows) {     people item = (people)row.databounditem;     if (item != null)     {         db.people.attach(item);     } } db.savechanges(); 

...but savechanges() did not recognize changes. however, if force every attached item "modified" state, works. not want change 100 items "modified", if 1 got modified.

any ideas?

edit 1 oh well, changed code keep context open time (or @ least long form gets displayed). now, ran different problem (people may have many jobs):

private void listbox1_selectedvaluechanged(object sender, eventargs e) {     people p = (people)listbox1.selecteditem;     if(p != null)     {         //jobbindingsource.clear(); caused error @ runtime...         db.entry(p).collection(b => b.job).load();         jobbindingsource.datasource = db.job.local.tobindinglist();     } } 

the datagridview bound jobbindingsource instance shows correct jobs person, in addition jobs selected person. tried clear() entries, if , click on same person twice, datagridview starts show no entries @ all. strange behaviour. doing wrong now?

edit 2 okay... found solution myself. refuse accept correct way it:

private void listbox1_selectedvaluechanged(object sender, eventargs e) {     people p = (people)listbox1.selecteditem;     if(p != null)     {         db.dispose();         db = new peoplejobsentities();         db.people.attach(p);         db.entry(p).collection(person => person.job).load();         jobbindingsource.datasource = db.job.local.tobindinglist();     } } 

only if dispose context , open anew, whole thing works. reason if clear local cache (of db.job.local), entries not reloaded again if use load() method. there way force reloading of entities?

while try not keep dbcontext open long period of time, datagrids don't have choice. set grid's datasource property iqueryable<t> , edits, deletes , additions taken care of grid , context itself. have call dbcontext.submitchanges() whenever want persist changes. can save each time user leaves row saving on rowleave or rowvalidated event. or can save when close form. make sure call dbcontext.dispose() when close form well.

to find out rows change can view changeset returned doing following:

var changes = dbcontext.getchangeset(); dbcontext.submitchanges(); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -