WCF Data Service - update record, not insert it

I am developing a WCF data service with self-monitoring objects, and I want clients not to insert duplicate content. Whenever they are POST data without providing a value for the data key, I have to follow some logic to determine if that data is already in my database or not. I wrote the Change interceptor as follows:

[ChangeInterceptor("MyEntity")] public void OnChangeEntity(MyEntity item, UpdateOperations operations){ if (operations == UpdateOperations.Add) { // Here I search the database to see if a matching record exists. // If a record is found, I'd like to use its ID and basically change an insertion // into an update. item.EntityID = existingEntityID; item.MarkAsModified(); } } 

However, this does not work. The existing EntityID is ignored and, as a result, the record is always inserted, never updated. Can this be done? Thanks in advance.

+4
source share
1 answer

Hooray! I have succeeded.

 item.EntityID = existingEntityID; this.CurrentDataSource.ObjectStateManager.ChangeObjectState(item, EntityState.Modified); 

I had to change the state of the object elsewhere, i.e. by calling .ChangeObjectState on the ObjectStateManager, which is a property of the underlying EntityContext. I was misled by the .MarkAsModified () method, which at the moment is not sure what it is doing.

+2
source

All Articles