You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
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:
This seems like it is the same problem as issue: #598
The problem is at this line:
EFCore.BulkExtensions/EFCore.BulkExtensions/DbContextBulkTransactionSaveChanges.cs
Line 103 in 7feed45
context.Database.CurrentTransaction
does not check Transactions from other places than those created via EF.The text was updated successfully, but these errors were encountered: