title: InvalidOperationException "Value of member 'X' of an object of type 'Y' changed." when saving data via WCF Data Services and LINQ
body: <p>I have a system that uses WCF Data Services on top of a .dbml (System.Data.Linq.DataContext) data model. The client retrieves data that it needs into a <code>System.Data.Services.Client.DataServiceContext</code>, makes changes, and saves back via WCF Data Services, which in turn saves to the SQL Server database. The <code>DataContext</code> class has an <code>IUpdateable</code> implementation very similar to <a href="http://msdn.microsoft.com/en-us/library/ee373841%28v=vs.110%29.aspx" rel="nofollow">this example</a>.</p>
  
  <p>I find this slightly weird and it was built a few years back so there's probably much better ways to do it now, but generally it's all been working well. However, recently it was deployed to a new environment and started having problems along the lines of </p>
  
  <pre><code>An exception of type 'System.Data.Services.Client.DataServiceRequestException' occurred and was caught.
  ... 
  Value of member 'BehaviorAsString' of an object of type 'profile' changed.
  A member that is computed or generated by the database cannot be changed.
  </code></pre>
  
  <p>The problematic column <code>BehaviorAsString</code> is calculated in the view that is the Source for the entity <code>profile</code>. In the dbml it's correctly set to ReadOnly=True, since it should never be updated. The stack trace shows that the problem is happening on the server side (as I interpret it - see below).  </p>
  
  <p>So far so straightforward: the problem appears to be that my UI is for some reason updating this property, then my DataService is rightly complaining that that's not allowed. </p>
  
  <p>However, I don't think that this is the case. That data model field is only used in one place, a .xaml file, where it's bound to a <code>TextBlock</code> (i.e. a readonly label). My WPF app isn't changing the field values anywhere. So I think the problem is the column change state gets lost when the objects are (de)serialized. The DataService on the server calls the setter on each field, thus all the fields are treated as dirty and would be saved back to the database. Then LinqToSql notices it's trying to save back a ReadOnly field and throws an exception. </p>
  
  <p>This means I need to do something on the server side to make it not see those readonly fields as changed, or otherwise prevent it from trying to save those changes. </p>
  
  <p>As an attempted hack at a solution I simply commented out the contents of the <code>BehaviorAsString</code> setter, so it would not be marked as changed. This fixed the immediate exception only for the same problem to occur on another ReadOnly calculated field on a different entity. It's also a <em>terrible</em> solution as it involves modifying an autogenerated file. </p>
  
  <p>It's a client environment so I have limited scope to install different versions and try out different things, so I'd like to have some good options before going back to them again.</p>
  
  <p>The really strange thing though is that this is only happening for this environment and I can't replicate it. It's a different build from one deployed at other clients, <s>but only a couple of small changes that aren't in files remotely related - I've triple checked this</s> <em>(NB: see update 3 below - could be because of cached DataContext)</em>. It's possible that there's a different .NET version installed on the server - the Application Pool is running in the .NET v2.0 runtime, with the app built using .NET 3.5 SP1 - or some other dependency that is different. No other referenced dlls are different.</p>
  
  <p>So my question is twofold: </p>
  
  <ol>
  <li><p>Given how I'm using WCF Data Services, what's the right way to deal with calculated fields and ensure LINQ doesn't try to save them to the database? </p></li>
  <li><p>Is there anything environmental that could have caused this problem to arise where it didn't before? </p></li>
  </ol>
  
  <p>And any other suggestions very welcome!</p>
  
  <hr>
  
  <p>Stack trace of the exception as it was logged on the client side: </p>
  
  <pre><code>Message: An exception of type 'System.Data.Services.Client.DataServiceRequestException' occurred and was caught.
  02/13/2014 16:05:20
  Type: System.Data.Services.Client.DataServiceRequestException, System.Data.Services.Client, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  Message: An error occurred while processing this request.
  InnerException(s):
    System.Data.Services.Client.DataServiceClientException: &lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"&gt;
    &lt;code&gt;&lt;/code&gt;
    &lt;message xml:lang="en-US"&gt;An error occurred while processing this request.&lt;/message&gt;
    &lt;innererror&gt;
      &lt;message&gt;Value of member 'BehaviorAsString' of an object of type 'profile' changed.&amp;#xD;
  A member that is computed or generated by the database cannot be changed.&lt;/message&gt;
      &lt;type&gt;System.InvalidOperationException&lt;/type&gt;
      &lt;stacktrace&gt;   at System.Data.Linq.ChangeProcessor.CheckForInvalidChanges(TrackedObject tracked)&amp;#xD;
     at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)&amp;#xD;
     at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)&amp;#xD;
     at System.Data.Linq.DataContext.SubmitChanges()&amp;#xD;
     at RPServices.RPDataModel.RPDataModelDataContext.SaveChanges()&amp;#xD;
     at System.Data.Services.DataService`1.BatchDataService.HandleBatchContent(Stream responseStream)&lt;/stacktrace&gt;
    &lt;/innererror&gt;
  &lt;/error&gt;
  
     at System.Data.Services.Client.DataServiceContext.SaveResult.&lt;HandleBatchResponse&gt;d__1e.MoveNext()
  Source: System.Data.Services.Client
  TargetSite: Void HandleBatchResponse()
  StackTrace:    at System.Data.Services.Client.DataServiceContext.SaveResult.HandleBatchResponse()
     at System.Data.Services.Client.DataServiceContext.SaveResult.EndRequest()
     at System.Data.Services.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)
     at RPAdminTool.ViewModel.ViewModelBase.SaveProcess()
     at RPAdminTool.ViewModel.ViewModelBase.&lt;Save&gt;b__2(Object _, DoWorkEventArgs __)
  </code></pre>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>Possibly <a href="http://social.msdn.microsoft.com/Forums/en-US/5bf3e5b0-e8a6-445a-9c7d-145d421d2fd1/linq-update-fails-for-concurrency-issue?forum=linqtosql" rel="nofollow">this</a> is related (?) which is referred to as a bug but with no reference or further explanation. </p>
  
  <p><strong>UPDATE 2:</strong> </p>
  
  <p>I've looked into <a href="http://msdn.microsoft.com/en-us/library/ee373841%28v=vs.110%29.aspx" rel="nofollow">IUpdateable</a> a little ... could I implement <code>IUpdatable.SaveChanges()</code> to clear changes on all fields marked as <code>IsDbGenerated=true</code>? </p>
  
  <p><strong>UPDATE 3:</strong> </p>
  
  <p>I did find a code change that I missed previously :-/ which meant that the client-side (WPF) code was re-using a static DataServiceChannel object (my RPDataModelDataContext object) instead of getting a separate one. This was causing another oddity I noticed where despite calling <a href="http://msdn.microsoft.com/en-us/library/dd756366%28v=vs.110%29.aspx" rel="nofollow"><code>DataServiceQuery.Expand()</code></a> some recently-created related entities weren't geting loaded. It turns out the re-use of the DataServiceChannel object meant it wasn't hitting the server/database to get the recently-created entities. </p>
  
  <p>I'm thinking this re-used DataServiceChannel object is <em>probably</em> the cause of this whole issue. I'm still not clear why it would have had this effect, but will update this question with the results of my testing. I'd be very interested in anyone able to answer <strong>why</strong> it would have this effect, as well as a general way to fix it on the server.</p>