This is my current code to update a field in database
private void SaveUser(User user)
{
USER UserObj = _db.USERs.First(i => i.USER_ID == user.USER_ID);
UserObj.NAME = user.NAME;
_db.SaveChanges();
}
This works fine. However, I want to change two things
1) Can I skip the loading of the row from DB first? i.e. this line USER UserObj = _db.USERs.First(i => i.USER_ID == user.USER_ID);
2) If there are 20 fields to update, how can I set all of them at once? Or I’ll have to write all 20 assignments myself?
Thanks
Edit – samples modified for EFv1 – it answers both your questions:
You are looking for this:
There is some difference between
EntityObjects (today mostly obsolete) and POCOs (only EFv4) where POCO simply updates if it is marked as modified butEntityObjectdoesn’t. It will update only if properties differ to its original state or if property is manually set as modified inObjectStateEntry.Selectively defining which properties must be updated is the same as manually assigning them:
Btw. have you heard about naming conventions?