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

发布于 2024-11-03 10:40:55 字数 600 浏览 6 评论 0原文

我正在开发具有自我跟踪实体的 WCF 数据服务,我想防止客户端插入重复的内容。每当他们发布数据而不提供数据键值时,我都必须执行一些逻辑来确定该数据是否已存在于我的数据库中。我写了一个像这样的 Change 拦截器:

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

但是,这不起作用。现有实体 ID 被忽略,因此,记录始终被插入,从不更新。甚至有可能做到吗?提前致谢。

I'm developing a WCF Data Service with self tracking entities and I want to prevent clients from inserting duplicated content. Whenever they POST data without providing a value for the data key, I have to execute some logic to determine whether that data is already present inside my database or not. I've written a Change interceptor like this:

[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 is not working. The existingEntityID is ignored and, as a result, the record is always inserted, never updated. Is it even possible to do? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2024-11-10 10:40:55

万岁!我成功做到了。

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

我必须在其他地方更改对象状态,即。通过调用 ObjectStateManager 的 .ChangeObjectState,它是底层 EntityContext 的属性。我被 .MarkAsModified() 方法误导了,目前我不确定它的作用。

Hooray! I managed to do it.

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

I had to change the object state elsewhere, ie. by calling .ChangeObjectState of the ObjectStateManager, which is a property of the underlying EntityContext. I was mislead by the .MarkAsModified() method which, at this point, I'm not sure what it does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文