LINQ中的实体附件问题
我试图从表单POST收到LINQ实体后,将LINQ实体附加到数据上下文。 但是,我得到的只是以下exception:
An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.
我也试过附加原始行,如下所示:
dataContext.People.Attach(person, originalPerson);
在这种情况下,我得到以下exception:
Object reference not set to an instance of an object.
这是我的控制器中的代码:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, Person person) { var prevPerson = dataContext.People.Single(p => p.ID == id); dataContext.People.Attach(person, prevPerson); dataContext.SubmitChanges(); return Redirect("~/People/Index"); }
关于我在这里做错了什么的想法? 如果需要,我可以发布实体代码。
在LinqToSQL设计器中,将所有更新检查设置为从不,当您附加调用时,如下所示:
context.entity.Attach(entity, true);
或者,您也可以从数据库中获取实体并使用POSTed实体中的数据进行更改,然后将其作为更改提交。
试试以下:
dataContext.People.Attach(person); dataContext.Refresh(RefreshMode.KeepCurrentValues, person); dataContext.SubmitChanges();
我通过在.Dbml文件和context.entity.Attach(entity, true);
中将UpdateCheck=Never
设置为我的属性来解决context.entity.Attach(entity, true);