I had to update single field in my database
using EF. I am doing as it like this:
1 - Load the orginal record from table based on name
2 - Update the field value
3 - save the object back to db
Code:
public void UpdateUser(List<User> users)
{
foreach (User user in users)
{
Job original = _context.Users.Where(x => x.Name == user.Name).AsNoTracking().FirstOrDefault();
if (original != null)
{
original.Name = user.Nanme;
_context.Entry(original).State = EntityState.Modified; // not sure it is needed or not
}
_context.SaveChanges();
}
}
Is it an efficient way? Or i can do it in much better way?