如何使用交易带的datacontext [英] How to use transactions with a datacontext

查看:147
本文介绍了如何使用交易带的datacontext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用交易,一个DataContext,这样我可以出错后回滚上下文的状态?如果是这样,这是如何工作的?

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?

推荐答案

一个DataContext将默认拿起一个环境事务,所以它只是一个确保有事情是在一个范围内的事务。细节成为主要问题:

A DataContext will pick up an ambient transaction by default, so it is just a matter of ensuring there is a Transaction in scope. The details become the main issue:


  • 您需要什么选项(例如隔离级别)

  • 你想要一个新的交易或重复使用现有的事务(如审计/日志操作可能需要一个新的事务,因此它可以即使的整体业务操作失败,因此外部事务回滚承诺)。

这是简化了一些原型代码,真正的代码使用助手创建具有政策推动期权交易(原型的目的之一是审查这些选项的影响)。

This is simplified some prototype code, the real code uses helpers to create the transactions with policy driven options (one of the purposes of the prototype was to examine the impact of these options).

using (var trans = new TransactionScope(
                           TransactionScopeOption.Required,
                           new TransactionOptions {
                               IsolationLevel = IsolationLevel.ReadCommitted
                           },
                           EnterpriseServicesInteropOption.Automatic)) {
    // Perform operations using your DC, including submitting changes

    if (allOK) {
        trans.Complete();
    }
}

如果完成()不叫则交易将回滚。如果有一个包含事务范围那么无论是内部和外部事务需要完成对被提交对数据库的更改。

If Complete() is not called then the transaction will be rolled back. If there is a containing transaction scope then both the inner and outer transactions need to Complete for the changes on the database to be committed.

这篇关于如何使用交易带的datacontext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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