CTP5:如何在UpdateException之后清除/重置DbContext [英] CTP5: How to clear/reset DbContext after UpdateException

查看:68
本文介绍了CTP5:如何在UpdateException之后清除/重置DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重置我的DbContext以使所有条目回滚到Unchanged状态?

How do I reset my DbContext such that all entries rollback to the Unchanged state?

我想要解决的方案是:撤消所有待处理的工作以响应用户决定取消操作。 操作顺序为:

The scenario I'm trying to solve is: undo all pending work in response to a user deciding to cancel an operation.  The sequence of actions is:


  1. 向多个DbSet添加新条目<>
  2. DbContext.SaveChanges()&NBSP; - 正确抛出SqlException,例如"违反PRIMARY KEY约束"
  3. <<此时我该怎么做才能忽略上述工作并恢复到已知状态? >>
  4. 做一些工作......这不会导致UpdateException
  5. DbContext.SaveChanges()  - 在步骤#3中没有做任何事情,DbContext在第1步仍然有不好的工作,并且抛出错误

很多

推荐答案

Q,

 

重置上下文对不同的人来说意味着不同的事情,并且需要采取不同的行动,具体取决于应该发生的事情。 
通常,完全重置上下文的最佳方法是不重置它,而是将其丢弃并以新的方式开始。 
如果这对您不起作用,那么您可以根据您的特定情况迭代实体并更改其状态,重置属性等。 
这实际上是一个非常复杂的过程,因为实体之间的修正可能会在某个实体发生某些事情时(例如FK)在某个实体发生某些事情时会发生变化(例如FK)。 
没有内置方法可以在特定点拍摄快照并重置到该点。

Resetting the context can mean different things to different people and will require different actions depending on exactly what should happen.  Typically the best way to reset the context fully is to not reset it but instead throw it away and start with a new one.  If this doesn’t work for you then you can iterate through entities and change their states, reset properties, etc as appropriate for your particular scenario.  This is actually quite an involved process because of things like fixup between entities that can change something (e.g. an FK) on one entity when something happens to another entity.  There is no built in way to take a snapshot at a particular point and reset to that point.

 

我可以提供一些示例代码,例如,删除已添加到上下文中的实体,如果它有帮助,但我不确定这将是
的最佳方法。

 

谢谢,

Arthur

 


这篇关于CTP5:如何在UpdateException之后清除/重置DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆