c# - How to update a record by id? -
i new entityframework , having trouble understand why first code doesn't update second does. aim update record without querying db.
is obvious?
using (chatdbentities db = new chatdbentities()) { // update buddy onlines buddy = new onlines(); buddy.id = 56; buddy.last_seen = datetime.now; buddy.status = (int)userstatuses.status.chatting; buddy.connected_to_id = 34; buddy.room_id = 2; db.savechanges(); } using (chatdbentities db = new chatdbentities()) { // update buddy var buddy = db.onlines.first(); buddy.last_seen = datetime.now; buddy.status = (int)userstatuses.status.chatting; buddy.connected_to_id = 34; buddy.room_id = 2; db.savechanges(); }
it looks related "id" primary identity key.
you have retrieve entity in order update it, can't create new 1 same id record database , expect ef magically know needs update database. try attaching objectcontext not how supposed updates.
Comments
Post a Comment