Skip to content

Explicit Database.BeginTransaction vs ambient transaction. #1110

@voroninp

Description

@voroninp

It should be mentioned that explicitly starting a database transaction via call DbContext.Database.BeginTransaction() will lead to exception if it is done inside ambient transaction scope.

The thrown exception is:

System.InvalidOperationException: 'An ambient transaction has been detected. The ambient transaction needs to be completed before beginning a transaction on this connection.'

Code:

using (var ambientTxn = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
using (var txn = await context.Database.BeginTransactionAsync())
{
}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions