gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:55 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 5,需要在单个事务中进行多个存储过程调用。它们都是插入,其中每个都依赖于前一个的输出,我希望能够回滚所有内容以防万一。每次调用都使用相同的 Entities 对象。第一次调用成功执行,但下一次调用总是失败并出现以下错误:

“底层提供者打开失败。”

内部异常:“分布式事务管理器 (MSDTC) 的网络访问已被禁用。请使用组件服务管理工具在 MSDTC 的安全配置中为网络访问启用 DTC。”

内部内部异常:“事务管理器已禁用其对远程/网络事务的支持。(HRESULT 异常:0x8004D024)”

有没有办法在不走 DTC 道路的情况下做到这一点?

代码:

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 ...
}
}

最佳答案

如果不启用 DTC,则无法执行此操作。

来自相关SO post :

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.

关于c# - 使用 Entity Framework 5 在 1 个事务中调用多个存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com