在 SubmitOperation 失败时保持 DomainContext 最新
背景: Silverlight 4 - RIA - Entity Framework 4
描述: 我有一些删除代码:
db.Items.Remove(selectedItem);
db.SubmitChanges(deleteItemOperationCompleted, null);
这里从域上下文中删除该项目,然后服务尝试在 EF 上执行操作(EF 又在 DB 上执行操作)。
回调方法:
private void deleteOperationCompletedM(SubmitOperation op)
{
if (op.Error == null)
{
MessageBox.Show("Delete operation was successfull.");
// Some other code here (removed for brevity)
}
else
{
op.MarkErrorAsHandled();
MessageBox.Show("An error has occured." + op.Error.Message);
}
}
重现: 我尝试删除该项目(由于数据库中的引用完整性约束而无法删除该项目)。我收到错误已发生的消息。没关系。然后,当我尝试删除某些其他项目(不通过外键与任何其他实体相关)时,我会收到相同的消息,即使可以从数据库中删除该项目。
问题是我已从域上下文中删除了第一项(即使它没有从数据库中删除)。因此,当我尝试删除第二项时,它也会从上下文中删除。提交更改时,可以从数据库中删除最后一项,但问题是更改是针对整个上下文提交的,并且由于在第一步中我删除了无法从数据库中删除的项目,提交操作失败。
问题:在提交失败的情况下“回滚”操作的正确方法(最佳实践)是什么?我唯一能想到的是创建另一个域上下文并再次加载数据,但由于加载的数据量,我想避免这种情况。上下文可以返回到之前的状态或取消挂起的更改吗?如何解决这个问题?
Background: Silverlight 4 - RIA - Entity Framework 4
Description:
I have some deletion code in which I do:
db.Items.Remove(selectedItem);
db.SubmitChanges(deleteItemOperationCompleted, null);
Here the item is removed from the domain context and then the service attempts to perform the operation on the EF (which in turn performs the operation on the DB).
The callback method:
private void deleteOperationCompletedM(SubmitOperation op)
{
if (op.Error == null)
{
MessageBox.Show("Delete operation was successfull.");
// Some other code here (removed for brevity)
}
else
{
op.MarkErrorAsHandled();
MessageBox.Show("An error has occured." + op.Error.Message);
}
}
Repro:
I try to delete the item (which can NOT be deleted because of the referential integrity constraint in the database). I receive the message that the error has occured. That's ok. When I then try to delete some other item (which is not related by foreign key to any other entity), I receive the same message, even though this item can be removed from the database.
The problem is that I have removed the first item from the domain context (even though it was not deleted from the database). So when I try to delete the second item, it is also removed from the context. When submiting the changes, the last item could be deleted from the DB, but the problem is that the changes are submitted for the whole context, and since in the first step I have removed an item that can't be deleted from the DB, the submit operation fails.
Question: What is the right way (best practice) to "rollback" the operation in the case of the submit failure? The only think I can think of is to create another domain context and to load the data again, but I'd like to avoid that because of the amount of data loaded. Can the context be returned to some previous state or cancel pending changes? How to approach this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,您应该调用 RejectChanges 在您的 DomainContext 上取消项目的删除并更改其 EntityState 返回“未修改”。
In your case, you should call RejectChanges on your DomainContext to cancel the deletion of the item and to change its EntityState back to Unmodified.