问题描述
我可以使用带有数据上下文的事务,以便在出错后回滚上下文的状态吗?如果是这样,它是如何工作的?
Can I use transactions with a datacontext, so that I can rollback the state of the context after an error? And if so, how does that work?
推荐答案
我一直在测试中使用它们 :)
I use them in testing all the time :)
try { dc.Connection.Open(); dc.Transaction = dc.Connection.BeginTransaction(); dc.SubmitChanges(); } finally { dc.Transaction.Rollback(); }
更新
这将始终在事后回滚.我在测试中使用它.
This will ALWAYS rollback after the fact. I use this in testing.