<p>Code below does not run correctly and throws <code>InvalidOperationExcepiton</code>.</p>

<pre><code>public void Foo()
{
 DataContext context = new DataContext();
 LinqEntity item = new LinqEntity(){ Id = 1, Name = "John", Surname = "Doe"} ;
 context.LinqEntities.Attach(item, true);
}
</code></pre>

[{'answer_id': 40079, 'author': 'Adam Lassek', 'author_id': 1249, 'author_profile': 'https://Stackoverflow.com/users/1249', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not sure what you mean by disconnected from the database.</p>\n\n<p>It appears that you are trying to insert a new row into the LinqEntities table -- is that correct?</p>\n\n<p>If that is the case you'll want to do</p>\n\n<pre><code>context.LinqEntities.InsertOnSubmit(item);\ncontext.Submit();\n</code></pre>\n"}, {'answer_id': 40108, 'author': 'Adam Lassek', 'author_id': 1249, 'author_profile': 'https://Stackoverflow.com/users/1249', 'pm_score': 1, 'selected': False, 'text': '<p>OK, if you\'re trying to update a row with ID = 1, you\'ll do it like this:</p>\n\n<pre><code>DataContext context = new DataContext();\nLinqEntity item = (from le in context.LinqEntities\n                  where le.ID == 1\n                  select le).Single();\nitem.Name = "John";\nitem.Surname = "Doe";\n\ncontext.Submit();\n</code></pre>\n\n<p>You could also replace the Linq expression with a more concise lambda:</p>\n\n<pre><code>LinqEntity item = context.LinqEntities.Single(le =&gt; le.ID == 1);\n</code></pre>\n\n<p>The most important thing the DataContext does is track any changes you make, so that when you call the Submit method it will autogenerate the Insert statements for the things you\'ve changed.</p>\n'}, {'answer_id': 40208, 'author': 'Scott Nichols', 'author_id': 4299, 'author_profile': 'https://Stackoverflow.com/users/4299', 'pm_score': 3, 'selected': True, 'text': '<p>By default, the entities will use all fields for checking concurrency when making edits.  That\'s what\'s throwing the InvalidOperationException.</p>\n\n<p>This can be setting the Update Check property for all fields to Never.  This must be done on all fields to attach the entity as modified.  If this is done, an additional call to context.SubmitChanges() will save the data.</p>\n\n<p>Alternatively, if you know the original values, you can attach and then make the updates, but all values that are being checked must match the original values.</p>\n\n<pre><code>LinqEntity item = new LinqEntity(){ Id = 1, Name = "OldName", Surname = "OldSurname"}; \ncontext.LinqEntities.Attach(item);\nitem.Name = "John";\nitem.Surname = "Doe";\ncontext.SubmitChanges();\n</code></pre>\n'}, {'answer_id': 40948, 'author': 'liammclennan', 'author_id': 2785, 'author_profile': 'https://Stackoverflow.com/users/2785', 'pm_score': 0, 'selected': False, 'text': '<p>When using an ORM you typically select an object before updating it. </p>\n\n<p>You can use <code>DataContext.ExecuteCommand(...)</code> to bypass the ORM if you do not want to do a select. </p>\n'}]
['https://Stackoverflow.com/questions/40054', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4215/']