How should I go about updating a row in the database? There is no update method, and if I use add and the primary key id already exists, I get an exception. Please provide an example if possible.
2 Answers
The easiest way is:
(1) retrieve existing row using pk.
(2) update properties.
(3) call SaveChanges() on context.
e.g.
var student = context.Students.Find(42); student.Description = "updated"; context.SaveChanges(); 1 Comment
Zoka
This is easiest way, but it makes one more query to the database doesn't it? I mean it must do the SELECT to find the entity you wanna update and then update it. But in general you should be able to do it without this query just with UPDATE. Is there such way?