efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Misleading Error Message from Microsoft.Data.Entity.Storage.RelationalConnection.CheckForAmbientTransactions()

Open michaelpaulus opened this issue 10 years ago • 6 comments

When using a transaction scope around EF7, I get an exception:

System.InvalidOperationException: An ambient transaction has been detected. Connections opened by Entity Framework will not be enlisted in ambient transactions. To suppress this warning call SuppressAmbientTransactionWarning() when overriding DbContext.OnConfiguring.

I see the discussion around this on issue: https://github.com/aspnet/EntityFramework/issues/1584

and the check-in code

https://github.com/aspnet/EntityFramework/commit/9195c1a013fedbbe6b0822ddcd752201b2bd0f2d

That added:

using (new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Suppress)) 

around the opening of a connection.

The following check-in that modified the RelationalConnection class https://github.com/aspnet/EntityFramework/commit/fef4009e557c4f5444cf2a2a7026140967d6dd4a removed this check.

In my testing I am able to use a transaction scope when I call SuppressAmbientTransactionWarning() and the connection created does enlist in the ambient transaction. This is the behavior I want, but is opposite of what the error message said.

Is there something I'm missing? Is it safe to SuppressAmbientTransactionWarning() and use the transaction scope, safe meaning you are not going to change the behavior to prevent me from using the transaction scope?

Thanks.

michaelpaulus avatar Dec 21 '15 16:12 michaelpaulus

@divega can you take a look at this one

rowanmiller avatar Jan 06 '16 19:01 rowanmiller

I think @michaelpaulus is right that the exception message is misleading: It is not that database connection won't be auto-enlisted in ambient transactions on Open() when using EF7 (this is actually not up to us and it will continue to happen on desktop .NET where System.Transactions exists and providers, e.g. SqlClient are designed to auto-enlist). It is rather than EF7's connection and transaction management logic is not aware of System.Transactions and hence lacks the necessary logic to deal with ambient transactions, e.g. it won't know not to start a local database transaction during SaveChanges() if there is an active ambient transaction.

divega avatar Jan 06 '16 20:01 divega

@divega to see what happens with us creating the nested transaction. If it works as expected then maybe we can just remove this exception.

rowanmiller avatar Jan 11 '16 19:01 rowanmiller

Instead of checking System.Transactions.Transaction.Current and throwing an exception, couldn't you save that the transaction exists in a local field and use that so you don't create your own transaction?

michaelpaulus avatar Jan 12 '16 15:01 michaelpaulus

Assigning to @anpete who is fixing the exception message for RTM as part of the fix for https://github.com/aspnet/EntityFramework/issues/5580.

I have created #5595 to track the lack of support for System.Transactions separately.

divega avatar May 31 '16 22:05 divega

Fixed.

anpete avatar Jun 01 '16 16:06 anpete