Search code examples
entity-frameworkado.netfieldupdating

ADO.Entity updating a single field of one record without retrieving the whole record


What is the best practise for updating a single field for one record (with specific ID) using ADO.Entity? As far as I know, you have to retrieve the whole object by id, update the property and call SaveChanges:

int id = ...;
var db = new MyEntities();
var o = (from mo in db.myObject
         where mo.id = idObject
         select mo).First();
o.MyProperty = "some value";
db.SaveChanges();

But it seems a little bit overhead having to retrieve the whole object, since I don't care for the values of the record because I just want to set a property, regardless of the values.

Another option would be to create a stored procedure for this purpose...


Solution

  • Re: What is the best practise for updating a single field for one record (with specific ID) using ADO.Entity?

    Answer: Best practice is to retrieve the entire record, update one or more fields, and then store the record. -- Just as you're doing.