vb.net - How do I update an entity in EF across multiple ASP.NET requests without retrieving it again? -


sounds easy, right? here's scenario...

private dbquery new reefentities  protected sub page_load(byval sender object, byval e system.eventargs) handles me.load     if not page.ispostback         currentcoral = (from c in dbquery.corals c.coralid = intcoralid).firstordefault        txtcommonname.text = currentcoral.commonname     end if end sub  protected sub btnsave_click(byval sender object, byval e system.eventargs) handles btnsave.click     'how access coral page load update it?    currentcoral.commonname = strname    dbquery.savechanges() end sub 

i don't want re-query result, want update query page load , save changes, right? how access original object update it?

http stateless protocol , result, every request make server needs rebuild object graph unless persist somewhere. there many ways persist data across web "session". in asp.net can store data in cookies, server side session, viewstate, form variables, , more.

first detach currentcoral object context when you're done in page_load

dbquery.detach(currentcoral) 

then put in data store view state.

me.viewstate.add("currentcoral", currentcoral) 

in next web request when save button clicked, retrieve entity view state , attach new object context.

currentcoral = ctype(me.viewstate("currentcoral"), coral) dbquery.attach(currentcoral) currentcoral.commonname = strname dbquery.savechanges() 

please forgive syntax errors. vb.net not first language! more details on attaching , detaching entities entity framework see following article.

attaching , detaching objects


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -