代码之家  ›  专栏  ›  技术社区  ›  changelog

LINQ中的实体附件问题

  •  16
  • changelog  · 技术社区  · 16 年前

    我试图在从表单帖子收到LINQ实体后将其附加到数据上下文。然而,我得到的只是以下例外:

    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);
    

    在本例中,我得到以下异常:

    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");
    }
    

    你知道我做错了什么吗?如果需要,我可以发布实体代码。

    3 回复  |  直到 16 年前
        1
  •  16
  •   Roman O    13 年前

    请尝试以下操作:

    dataContext.People.Attach(person);
    dataContext.Refresh(RefreshMode.KeepCurrentValues, person);
    dataContext.SubmitChanges();
    
        2
  •  14
  •   Benjamin Autin Andrés Bonilla    16 年前

     context.entity.Attach(entity, true);
    

    或者,您也可以从数据库中获取实体,并使用发布实体中的数据对其进行更改,然后将其作为更改提交。

        3
  •  0
  •   Manos Nikolaidis Airsource Ltd    9 年前

    我通过设置解决了这个问题 UpdateCheck=Never 到.Dbml文件中的我的属性,然后 context.entity.Attach(entity, true);