TransactionScope transaction suppression in read-only context in EF?

I have a scenario where I need to open several datacontexts that point to different databases. I just write to one of the databases and read from the others ... so technically the transaction should only be against one of the databases.

I would like to avoid updating TransactionScope in a distributed transaction, so I don't need to worry about MSDTC ... is there a way to use only one context in a transaction?

+4
source share
2 answers

I considered a similar problem with Linq to Sql - initially our solution should use the same connection for each query. Rick Strahl made a series of blog posts worth paying attention to.

In our solution, the DataContext constructor has an overloaded constructor that retrieves the connection from the factory (if there is no connection, the transferred connection is stored in the stream)

public DataContext1(connection) : base (ConnectionFactory.GetConnectionFromContext(connection)) { } 

This is similar to working in a WCF script, a factory connection can store / retrieve from the ServiceModel.OperationContext.Items collection and (more importantly) subscribe to the OperationComplete event to close / delete the connection to the stream when all operations are performed.

I found that we also need to expand the connection object so that you can prevent the internal connection from being closed / dipole automatically when the disposable datacontext is placed (for example, after the completion of each workspace).

Now I'm watching scenarios not related to WCF ... TBH is not very. We also do not have an OperationCompleted event trigger that is present in the context of WCF.

+1
source

I'm not sure if this will get the second connection into the transaction, but you can try to suppress the transaction of your choice:

 using (new TransactionScope(TransactionScopeOption.Suppress)) {} 
+2
source

All Articles