I am experiencing this weird behavior where the transaction gets committed only when the using
exits and not when calling scope.Complete();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
scope.Complete();
// data still doesn't show in db
}
// now shows in db
How do I commit the transaction before exiting the using
statement?
The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically.
TransactionScope provides an implicit programming model in which the transactions are automatically managed by the infrastructure. It provides a simple mechanism for you to specify a code block to participate in a transaction.
A transaction which automatically identifies a code block that needs to support a transaction without explicitly mentioning any transaction related things. An ambient transaction is not tied just to a database, any transaction aware provider can be used. TransactionScope implements an ambient transaction.
Entity Framework is already operating within a TransactionScope. The connection object in the transaction passed is null. That is, the transaction is not associated with a connection – usually this is a sign that that transaction has already completed.
from the documentation:
The actual work of commit between the resources manager happens at the End Using statement if the TransactionScope object created the transaction.
So it doesn't look like you can truly commit the transaction before the using statement end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With