Alternative to TransactionScope of System.Transaction assembly in .net core framework

后端 未结 2 891
难免孤独
难免孤独 2021-01-03 23:07

The System.Transaction assembly is not part of the .net core framework at the moment (see https://github.com/dotnet/corefx/issues/2949). In my application (asp.net core mvc)

相关标签:
2条回答
  • 2021-01-03 23:55

    Just wanted to leave a comment here in case anybody wandered by this post while researching this issue. I ran into this randomly on .net core 2.1 using Dapper, which includes System.Data.SqlClient 4.5.0 by default. Adding version System.Data.SqlClient 4.5.1 independently via nuget worked. So it seems this has been fixed as of 4.5.1.

    The following are the relevant sections of my csproj file:

    <ItemGroup>
       <PackageReference Include="Dapper" Version="1.50.5" />
       <PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
    </ItemGroup>
    
    0 讨论(0)
  • 2021-01-04 00:01

    Update 2 .NET Core 2.0 is out now. You can use this API. See https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transactionscope?view=netcore-2.0

    Update System.Transactions will be available in NET Core 2.0. See https://github.com/dotnet/core/blob/master/roadmap.md for details on upcoming releases.

    Original answer

    System.Transactions (or ambient transactions) is not implemented in .NET Core 1.0.0 but may be implemented in future versions.

    You can work around this by using explicit transactions.

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
    
                using (var transaction = connection.BeginTransaction())
                {
                   // transaction.Commit();
                   // transaction.Rollback();
                }
            }
    
    0 讨论(0)
提交回复
热议问题