WCF数据服务-更新记录而不是插入记录

WCF数据服务-更新记录而不是插入记录,wcf,entity-framework,odata,wcf-data-services,Wcf,Entity Framework,Odata,Wcf Data Services,我正在开发一个具有自跟踪实体的WCF数据服务,我希望防止客户端插入重复的内容。每当他们在没有为数据键提供值的情况下发布数据时,我必须执行一些逻辑来确定该数据是否已经存在于我的数据库中。我编写了一个变更拦截器,如下所示: [ChangeInterceptor("MyEntity")] public void OnChangeEntity(MyEntity item, UpdateOperations operations){ if (operations == UpdateOperations

我正在开发一个具有自跟踪实体的WCF数据服务,我希望防止客户端插入重复的内容。每当他们在没有为数据键提供值的情况下发布数据时,我必须执行一些逻辑来确定该数据是否已经存在于我的数据库中。我编写了一个变更拦截器,如下所示:

[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();
  }
}

然而,这是行不通的。将忽略existingEntityID,因此记录始终插入,从不更新。甚至可以这样做吗?提前谢谢。

万岁!我设法做到了

item.EntityID = existingEntityID;
this.CurrentDataSource.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
我必须在别处更改对象状态,即调用ObjectStateManager的.ChangeObjectState,它是底层EntityContext的属性。我被.MarkAsModified()方法误导了,现在我不确定它是做什么的