c# - Do I need to specify add or attach -


in case of needed add new row 1-1 relationship, need specify add or attach? , how do if need to?

//one tblcontent 1 tblcontentdata //updating tblcontentdata corresponding particular id in tblcontent  int id = 12345; tblcontent entity = db.tblcontents.where(con => con.id == id)                       .firstordefault(); if (entity == null)    throw new exception("id bad");  if (entity.tblcontentdata == null)    entity.tblcontentdata = new tblcontentdata();  //proceed updating foreign keyed table 

add new rows. add not updates. in code posted, relationship between entity , context should preserved need call:

db.savechanges();

to preserve updates.

in cases relationship broken can update item entry:

   db.entry(entity).state = entitystate.modified;    db.savechanges();    

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -