WCF 数据服务 - 更新记录而不是插入记录
我正在开发具有自我跟踪实体的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
万岁!我成功做到了。
我必须在其他地方更改对象状态,即。通过调用 ObjectStateManager 的 .ChangeObjectState,它是底层 EntityContext 的属性。我被 .MarkAsModified() 方法误导了,目前我不确定它的作用。
Hooray! I managed to do it.
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.