无法使用Entiy Framework开始分布式事务 [英] Unable to begin a distributed transaction using Entiy Framework

查看:433
本文介绍了无法使用Entiy Framework开始分布式事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private bool _updateList(SysInfo _sysInfo,List< pList> ; _pList)
{
try
{

foreach(_ pList中的var p)
{
_context.spUpdatePListApprovalFlow(p.countryID, _sysInfo.User.JobRoleID,p.src,p.id,p.status,_sysInfo.User.Username);
}

返回true;
}
catch(Exception ex)//仅调试
{
throw; //抛出错误到主要尝试catch
}
}

错误


无法执行操作,因为OLE DB提供程序MSDASQL
用于链接服务器AS400_LINKEDSRV 无法开始分发的
事务。


但是,当我在SQL管理中运行存储过程时,一切正常Studio:

  exec [dbo]。[spUpdatePListApprovalFlow] 
@CountryID = 123456,
@UserTypeID = 23456,
@Src = 1,
@Id ='123456789',
@Status = 30,
@Username ='username'

我厌倦了挖掘一个答案没有任何作用...我尝试过的很少的东西:




  • SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

  • 在存储过程中插入多个事务



这是p上有2个子存储过程。一个写入应用程序数据库的表,另一个更新为as400中的表。

解决方案

在EF6存储过程中默认情况下,在显式事务中调用。您可以通过在创建它之后更改其配置,或通过在构造函数中更改DbContext类型的所有实例来关闭特定的DbContext实例。 EG

  using(var db = new Db())
{
db.Configuration.EnsureTransactionsForFunctionsAndCommands =假;
//。 。 。
}

请参阅: https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure。 dbcontextconfiguration.ensuretransactionsfor功能和命令


I'm having the following error executing this piece of code:

private bool _updateList(SysInfo _sysInfo, List<pList> _pList)
{
    try
    {

        foreach (var p in _pList)
        {
            _context.spUpdatePListApprovalFlow(p.countryID, _sysInfo.User.JobRoleID, p.src, p.id, p.status, _sysInfo.User.Username);
        }

        return true;
    }
    catch (Exception ex) //debug only
    {
        throw; //throws error to the main try catch
    }
}

ERROR

The operation could not be performed because OLE DB provider "MSDASQL" for linked server "AS400_LINKEDSRV" was unable to begin a distributed transaction.

However, everything works fine when I run the Stored Procedure in SQL Management Studio:

exec [dbo].[spUpdatePListApprovalFlow]
        @CountryID  = 123456,
        @UserTypeID = 23456,
        @Src = 1,
        @Id = '123456789',
        @Status = 30,
        @Username  = 'username'

I'm tired of digging for an answer nothing works... Few things I've tried:

  • SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  • insert multiple transactions in the stored procedure

This sp has 2 sub stored procedures on it. One that writes into a table of the application's database, and another that updates a table in as400.

解决方案

In EF6 stored procedures are called in an explicit transaction, by default. You can turn this off for a particular DbContext instance by changing its configuration after creating it, or for all instances of a DbContext type by changing it in the constructor. EG

    using (var db = new Db())
    {
        db.Configuration.EnsureTransactionsForFunctionsAndCommands = false;
        //. . .
    }

See: https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcontextconfiguration.ensuretransactionsforfunctionsandcommands

这篇关于无法使用Entiy Framework开始分布式事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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