c# - EF 4.0 Self-Tracking Entities, intended updates are being translated into inserts -
let's assume below method lives in wcf service. ui retrieved instance of status object, , makes subsequent call service using method. instead of assigning status user expect, attempts insert status. doing wrong?
void method(status status) { //not sure if needed, status never changed context.statuses.applychanges(status); //get first user database user user = context.users.where(u => u.id = 1).first(); //set user status existing status user.status = status; //this throws exception due ef trying insert new entry //into status table, rather updating user.statusid column. context.savechanges(); }
the problem working attached user. when ste attached context behaves in same way other entity. more on self tracking mechanism not activated. must attach status context before set user or tracked new entity has inserted:
void method(status status) { user user = context.users.where(u => u.id = 1).first(); context.attach(status); user.status = status; context.savechanges(); }
Comments
Post a Comment