c# - Updating one record triggers changes of all the records for one of the same primary key in Entity Framework 6 -
i used ado.net entity data model entity framework of local db in vs project. there 1 table 2 composite primary keys (userid, data) , 1 modifiable field (status).
the problem is, when try retrieve 1 specific record , modify "status" field, "status" same userid change regardless value of data. (however records under different userid stay unaffected.) updating codes follows:
public void updatestatus(string userid, string data, short status) using (var context = new mydbcontext()) { var data = context.userdatastatus.single(s => s.userid == userid && s.data == data); data.status = status; context.userimagestatus.attach(data); context.entry(data).property(p => p.status).ismodified = true; context.savechanges(); } } i tried many different ways update record, such using .where().firstdefault() retrieve record instead of single(), or adding .asnotracking() after context.userdatastatus. didn't work either.
i found entity framework doesn't handle correctly duplicated / no primary keys, example, have add .asnotracking() in query workaround when retrieving data same db. wonder if same reason cause updating problem, , how solve it?
====
updated:
well, i've found problem caused automatically generated ef models gave userid primary key. though chose "update model database" in .edmx, primary key didn't updated @ all. went .edmx model browser, manually update property of "data" field, , problem solved. , don't have use .asnotracking() in query anymore, too.
well, i've found problem caused automatically generated ef models gave userid primary key. though chose "update model database" in .edmx, primary key didn't updated @ all. went .edmx model browser, manually update property of "data" field (to set primary key), , problem solved. , don't have use .asnotracking() in query anymore, too.
Comments
Post a Comment