Sam*_*mWM 8 c# entity-framework asp.net-mvc-2
我使用LINQ to Entities设置了一个模型,并且代码可以按预期添加到数据库中.但是,当我使用.NET 3.5时,我无法使UpdateModel工作.
[HttpPost]
public ActionResult Edit(Site.Models.XYZ xyz)
{
    try
    {
        var original = db.XYZ.First(u => u.id == xyz.id);
        UpdateModel(original);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch (Exception ex)
    {
        return View("Error");
    }
}
Run Code Online (Sandbox Code Playgroud)
这导致以下异常:
System.InvalidOperationException was caught
  Message=The model of type 'Site.Models.XYZ' could not be updated.
  Source=System.Web.Mvc
  StackTrace:
       at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
       at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix)
       at Site.Controllers.XYZController.Edit(Site.Models.XYZ xyz) in D:***.cs:line 81
  InnerException: 
Run Code Online (Sandbox Code Playgroud)
如果我这样做UpdateModel(xyz),则不会发生异常,但数据也不会保存.
如何让UpdateModel使用它(不更新到.NET 4.0),为什么不能更新(由于没有内部异常,异常没有帮助)?
Sam*_*mWM 13
管理解决问题.可以通过以下两种方式之一完成:
TryUpdateModel(original)
Run Code Online (Sandbox Code Playgroud)
要么
db.ApplyPropertyChanges(original.EntityKey.EntitySetName, xyz)
Run Code Online (Sandbox Code Playgroud)
不知道为什么TryUpdateModel会工作但UpdateModel不会.也许只是.NET 3.5中的一个错误.