C首先的TransactionScope EF5 $ C $而不升级到MSDTC [英] EF5 Code First TransactionScope without escalating to MSDTC

查看:242
本文介绍了C首先的TransactionScope EF5 $ C $而不升级到MSDTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用EF5 code首先,我需要在同一个事务包裹多次调用的SaveChanges()。我是比较新的使用的TransactionScope,并且似乎无法调整它的选项来完成我所希望做的。

I'm using EF5 code first, and I need to wrap multiple calls to SaveChanges() in the same transaction. I'm relatively new to using transactionScope, and can't seem to tweak the options on it to accomplish what I'm looking to do.

下面code在于获得与我的DbContext类的一个实例注入的服务类。我没有在这个类中的任何位置创建的DbContext的其他实例。

The following code resides in a service class that gets injected with an instance of my DbContext class. I do not create additional instances of DbContext anywhere in this class.

起初,我尝试以下。在code是非常干净的,但它试图升级到到DTC在的SaveChanges 的方法 GenerateInvoice 功能。

At first I tried the following. The code is nice and clean, but it attempts to escalate to to DTC during the SaveChanges method in the GenerateInvoice function.

using (var scope = new TransactionScope())
{
    // This function does some work and calls SaveChanges
    Guid invoiceId = GenerateInvoice(cart);  

    // This function also does some querying and calls SaveChanges
    DeleteCart();

    // Transaction should only commit if both functions above were successful
    scope.Complete();

}

我做了一些研究,改变了code到不升级到DTC以下,但我不知道这是否是最好的办法。

I did some research and changed the code to the following which does not escalate to DTC, but I'm not sure if it's the best approach.

IObjectContextAdapter adapter = (IObjectContextAdapter)_context;

adapter.ObjectContext.Connection.Open();

using (var scope = new TransactionScope())
{

    // This function does some work and calls SaveChanges
    Guid invoiceId = GenerateInvoice(cart);  

    // This function also does some querying and calls SaveChanges
    DeleteCart();

    // Transaction should only commit if both functions above were successful
    scope.Complete();

    adapter.ObjectContext.Connection.Close();

}

有没有一种简单的方法来获取与多个EF5 code首先事务支持的SaveChanges 没有升级到DTC?

Is there a simpler way to get transaction support with EF5 code first across multiple SaveChanges without escalating to DTC?

感谢

推荐答案

不,我已经找到。它是有道理的为好。通过调用的SaveChanges()不止一次,你可能打开了多个连接到数据库。协调事务跨越这些连接的唯一方法是与分布式事务(因此MSDTC)。

Not that I have found. And it makes sense as well. By calling SaveChanges() more than once, you are potentially opening up multiple connections to the database. The only way to coordinate the transaction across these connections is with a distributed transaction (hence MSDTC).

我能想出的最好的解决办法是只调用的SaveChanges()键。这可能意味着重组约code,但它是最好的,如果全部都需要是事务性的所有实体提交在同一时间。

The best solution I could come up with was to only call SaveChanges() once. This might mean restructuring some code, but it is best if all the entities that are needed to be transactional all commit at the same time.

只有调用的SaveChanges()键也可以让EF变更跟踪使用它的缓存,节省了大量的应用程序和数据库之间的通信。

Only calling SaveChanges() once also allows the EF change tracker to use its cache and save a lot of communication between the app and the db.

这篇关于C首先的TransactionScope EF5 $ C $而不升级到MSDTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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