Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BulkSaveChanges throws An ambient transaction has been detected. The ambient transaction needs to be completed before beginning a transaction on this connection.' #1425

Open
SpaceOgre opened this issue Mar 4, 2024 · 2 comments

Comments

@SpaceOgre
Copy link

SpaceOgre commented Mar 4, 2024

I changed from SaveChanges to BulkSaveChanges for one of our methods that does lots of inserts, it works fine when running the code in production but not during our integration tests since we wrap those in a TransactionScope. We get this error:

System.InvalidOperationException: An ambient transaction has been detected. The ambient transaction needs to be completed before starting a new transaction on this connection.
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.EnsureNoAmbientOrEnlistedTransactions()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.EnsureNoTransactions()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransaction(IsolationLevel isolationLevel)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransaction()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.BeginTransaction()
   at EFCore.BulkExtensions.DbContextBulkTransactionSaveChanges.SaveChangesAsync(DbContext context, BulkConfig bulkConfig, Action`1 progress, Boolean isAsync, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.DbContextBulkTransactionSaveChanges.SaveChangesAsync(DbContext context, BulkConfig bulkConfig, Action`1 progress, Boolean isAsync, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.DbContextBulkTransactionSaveChanges.SaveChangesAsync(DbContext context, BulkConfig bulkConfig, Action`1 progress, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.DbContextBulkTransaction.ExecuteAsync[T](DbContext context, Type type, IEnumerable`1 entities, OperationType operationType, BulkConfig bulkConfig, Action`1 progress, CancellationToken cancellationToken)
   at GR.Elevinformation.Library.Features.Elinfiler.PostElinfilHandler.HandleAsync(PostElinfilRequest request, CancellationToken ct) in C:\Users\Nordstrom\source\GR\Elevinformation.API\source\GR.Elevinformation.Library\Features\Elinfiler\PostElinfilHandler.cs:line 43
   at GR.Elevinformation.API.Features.Elinfiler.PostElinfilEndpoint.HandleAsync(PostElinfilRequest req, CancellationToken ct) in C:\Users\Nordstrom\source\GR\Elevinformation.API\source\GR.Elevinformation.API\Features\Elinfiler\PostElinfilEndpoint.cs:line 29
   at FastEndpoints.Endpoint`2.ExecAsync(CancellationToken ct)
   at FastEndpoints.Endpoint`2.ExecAsync(CancellationToken ct)
   at NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware.Invoke(HttpContext context)
   at NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext context)
   at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Serilog.AspNetCore.RequestLoggingMiddleware.Invoke(HttpContext httpContext)

This seems like it is the same problem as issue: #598

The problem is at this line:

var transaction = context.Database.CurrentTransaction ?? context.Database.BeginTransaction();

context.Database.CurrentTransaction does not check Transactions from other places than those created via EF.

@filmstarr
Copy link
Contributor

I've been getting the same issue DbContextBulkTransactionSaveChanges too. I've submitted a pull request with my proposed fix for this.

@borisdj
Copy link
Owner

borisdj commented Mar 4, 2024

PR merged, thx for contrib.
Will be released with next nuget.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants