dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.63k stars 1.96k forks source link

Overview of EF Core and Transactions Does Not Discuss Async #3250

Open richardcox13 opened 3 years ago

richardcox13 commented 3 years ago

In EF Framework, when using transaction scopes with awaitable operations it was necessary to pass the TransactionScopeAsyncFlowOption .Enabled flag.

This removed the thread affinity of the scope, so should Task continations execute on a different thread everything would work. Because thread switches cannot be controlled and tended not to happen in a development environment, this flag needed to be used even when under the debugger the exceptions would not be thrown.

In EF Core is this needed? Does DbContext.Database.BeginTransaction() handle the async case?

As async is the generally preferred approach it would really help if the documentation made it clear if this is not needed because many developers using EF Core will have learnt EF before Core was started let alone the current platform.


Document Details

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

roji commented 3 years ago

@richardcox13 TransactionScopeAsyncFlowOption controls whether the ambient transaction started by TransactionScope (System.Transactions) is flowed across threads when performing async operations (with await). When you use DbContext.Database.BeginTransaction (or its async counterpart, BeginTransactionAsync), you aren't using TransactionScope at all - the two transaction management mechanisms are completely unrelated, and should not be mixed.

So to summarize... If you're using TransactionScope (System.Transactions) to manage your transactions, and are using async, then yes, you probably need to specify TransactionScopeAsyncFlowOption.Enabled. If, instead, you're using the BeginTransaction APIs, then there is no TransactionScope and TransactionScopeAsyncFlowOption.Enabled isn't needed.

richardcox13 commented 3 years ago

@roji this information should be in the documentation.: the referenced page does not mention async, but that is the preferred usage model.

The rest of this issue is what, IMHO, should be answered within the page.

roji commented 3 years ago

@richardcox13 the (non-)relationship between System.Transactions/TransactionScope and manual transaction management via BeginTransaction is a general .NET database thing rather than an EF Core thing. But I agree making this clear on our docs could be useful.

AndriySvyryd commented 3 years ago

We should link to System.Transactions/TransactionScope docs