DatabaseFacade.BeginTransactionAsync(CancellationToken) Method

Definition

Asynchronously starts a new transaction.

public virtual System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction> BeginTransactionAsync (System.Threading.CancellationToken cancellationToken = default);
abstract member BeginTransactionAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction>
override this.BeginTransactionAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction>
Public Overridable Function BeginTransactionAsync (Optional cancellationToken As CancellationToken = Nothing) As Task(Of IDbContextTransaction)

Parameters

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

A task that represents the asynchronous transaction initialization. The task result contains a IDbContextTransaction that represents the started transaction.

Exceptions

Remarks

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Transactions in EF Core for more information and examples.

Applies to