2

Controller

[HttpPost]
public ActionResult EditUserProfile(UserProfiles _postedUserProfile)
{
    UserProfiles orjinalUserProfile = entity.UserProfiles.Where(x => x.UserId == _postedUserProfile.UserId).Single();
    orjinalUserProfile.AboutMe = _postedUserProfile.AboutMe;
    orjinalUserProfile.Birthday = _postedUserProfile.Birthday;
    orjinalUserProfile.Comments = _postedUserProfile.Comments;
    ...... // there are lines more 
    entity.SaveChanges();
    return View();
}

I updated entity like above. But I think this way is not good. Is There any solutoin to update entity in a line, for example insert operation like entity.AddToUserProfiles .

Thanks.

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197

1 Answers1

0

You can use the ApplyCurrentValues method.

Here is an example: ApplyCurrentValues in EF 4

Basically:

[HttpPost]
public ActionResult EditUserProfile(UserProfiles _postedUserProfile)
{
    UserProfiles orjinalUserProfile = entity.UserProfiles.Where(x => x.UserId == _postedUserProfile.UserId).Single();
    entity.UserProfiles.ApplyCurrentValues(_postedUserProfile);
    entity.SaveChanges();
    return View();
}
Community
  • 1
  • 1
echo
  • 7,737
  • 2
  • 35
  • 33
  • I try this. But there is no change on database. I cant understand How do orjinal and modified entity matches between them in above code? – AliRıza Adıyahşi Aug 14 '12 at 23:54
  • i noticed a mistake with my original post. I was passing orjinalUserProfile to the ApplyCurrentValues method, instead of _postedUserProfile. I've edited to reflect the change. Try using this object instead. – echo Aug 16 '12 at 02:45
  • It still does not work. Should we ensure relationship between orjinal and posted entity? – AliRıza Adıyahşi Aug 16 '12 at 21:54
  • Yes, i assumed they had the same primary key value. – echo Aug 24 '12 at 02:24