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
Post a Comment