17

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.

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

Any ideas on what I'm doing wrong here? I can post the entity code if needed.

changelog
  • 4,646
  • 4
  • 35
  • 62
  • Which line / object is raising the "Object reference not set to an instance of an object" error? I'd guess that prevPerson is null (maybe ID not being mapped as you're expecting?) but it's hard to say without seeing the code... – Dylan Beattie Feb 11 '09 at 14:14
  • It's all being mapped. It throws the error when I Attach() the entity to the Table object. – changelog Feb 11 '09 at 16:32

3 Answers3

16

Try following:

dataContext.People.Attach(person);
dataContext.Refresh(RefreshMode.KeepCurrentValues, person);
dataContext.SubmitChanges();
Roman O
  • 3,172
  • 30
  • 26
  • This is the solution that worked best for me. No need to set the `UpdateCheck=Never` on all properties – BLSully Apr 19 '13 at 15:05
  • worked perfectly, for attached and not attached entities – Albert Cortada Jan 12 '15 at 11:54
  • 1
    This does work, but it loads up the object from the database before saving it. I don't want the extra load, that's why I'm attaching something that I've previously loaded. – Jon Kruger Sep 09 '15 at 20:34
  • It's years later but wanted to add that this results in "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported." – Richard Griffiths Aug 31 '16 at 11:26
  • So, to follow up with Jon Kruger's "Sep 9 '15 at 20:34" Comment, is there a way to avoid the "extra load" and just force it to update all Columns (without having to "set the UpdateCheck (Attribute)=Never on all properties" which I believe would require (in SQL Metal) changes to auto-generated code)? The DataContext that I retrieve the Entity from had already been Closed & and Disposed of at update / delete time (because we Open, Close and Dispose of the DataContext with each Db access). – Tom Sep 17 '18 at 22:31
14

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.

Benjamin Autin
  • 4,143
  • 26
  • 34
  • I've run into the same problem doing something similar. – Benjamin Autin Feb 13 '09 at 15:09
  • That Call results in an "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."} System.Exception {System.InvalidOperationException}." for me. @Roman O's Answer dated "Jan 30 '12 at 12:44" worked for me. NOTE: The `DataContext` that I retrieved the Entity from had already been Closed & and Disposed of (because we Open, Close and Dispose of the `DataContext` with each Db access). – Tom Sep 17 '18 at 22:28
0

I solved by setting UpdateCheck=Never to my property in .Dbml file and context.entity.Attach(entity, true);

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82