在LINQ实体连接问题 [英] Entity attachment issues in LINQ
问题描述
我想我从一个表单POST接收后,到LINQ实体连接到数据上下文。但是,我得到的是以下异常:
I am trying to attach a LINQ entity to the data context after I receive it from a form POST. However, all I get is the following 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.
我也尝试安装原始行,像这样:
I have also tried attaching the original row, like so:
dataContext.People.Attach(person, originalPerson);
在这种情况下,我得到了以下异常:
In this case, I get the following exception:
Object reference not set to an instance of an object.
下面是我在控制器中的code:
Here's the code in my controller:
[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");
}
这是我做错了什么在这里的任何想法?如果需要的话我可以张贴实体code。
Any ideas on what I'm doing wrong here? I can post the entity code if needed.
推荐答案
在LinqToSQL设计器中设置的所有检查更新为从不,当你连接调用它像这样:
In the LinqToSQL designer set all of the Update Checks to Never and when you attach call it like so:
context.entity.Attach(entity, true);
另外,你也可以抓住从数据库实体,并使用从发布的实体中的数据进行更改,然后提交,作为一个改变。
Alternatively, you could also grab the entity from the db and change it using the data from the POSTed entity, then submit that as a change.
这篇关于在LINQ实体连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!