2

控制器

[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();
}

我像上面一样更新了实体。但我认为这种方式并不好。是否有任何解决方案来更新一行中的实体,例如像entity.AddToUserProfiles.

谢谢。

4

1 回答 1

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();
}
于 2012-08-14T23:41:34.537 回答