简体   繁体   English

使用Entity Framework 5在1个事务中调用多个存储过程

[英]Multiple stored procedure calls in 1 transaction with Entity Framework 5

I am using Entity Framework 5 and need to make multiple stored procedure calls in a single transaction. 我正在使用Entity Framework 5,并且需要在单个事务中进行多个存储过程调用。 They are all inserts where each depends on an output of the previous one and I want to be able to roll everything back in case one fails. 它们都是插入,每个插入都取决于前一个的输出,我希望能够回滚所有内容,以防万一失败。 Each call uses the same Entities object. 每个调用使用相同的Entities对象。 The first call executes successfully, but the next one always fails with the following error: 第一个调用成功执行,但是下一个调用始终失败,并显示以下错误:

"The underlying provider failed on Open." “底层提供程序在Open上失败。”

Inner exception: "Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool." 内部例外:“已禁用分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具在MSDTC的安全配置中为网络访问启用DTC。”

Inner Inner exception: "The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)" 内部内部异常:“事务管理器已禁用对远程/网络事务的支持。(HRESULT的异常:0x8004D024)”

Is there a way to do this without going down the DTC road? 有没有办法在不走DTC道路的情况下做到这一点?

Code: 码:

ObjectParameter outputParam1 = new ObjectParameter("newEntity1", 0);
ObjectParameter outputParam2 = new ObjectParameter("newEntity2", 0);

using (var scope = new TransactionScope())
{
    try
    {
        using(var context = new DatabaseContext())
        {
            context.f_createEntity1(outputParam1);
        }

        using(var context = new DatabaseContext())
        {
            context.f_createEntity2(ouputParam2, outputParam1.Value);
        }

        ... other calls ...

        scope.Complete();
    }
    catch (Exception ex)
    {
        ... handle error ...
    }
}

There's not a way to do this without having the DTC enabled. 如果未启用DTC,则无法执行此操作。

From a related SO post : 从相关的SO帖子中

At least two durable resources that support single-phase notifications are enlisted in the transaction. 事务中至少需要两个支持单阶段通知的持久资源。 For example, enlisting a single connection with does not cause a transaction to be promoted. 例如,仅注册一个连接不会导致事务升级。 However, whenever you open a second connection to a database causing the database to enlist, the System.Transactions infrastructure detects that it is the second durable resource in the transaction, and escalates it to an MSDTC transaction. 但是,每当您打开与数据库的第二个连接导致该数据库加入时,System.Transactions基础结构都会检测到它是事务中的第二个持久资源,并将其升级为MSDTC事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM