When I make a context and in it I first make a series of requests to one database, and then a request to another database, an error occurs that distributed transactions on the server are prohibited.

How can I make sure that the second transaction does not fall into this Transaction Scope?

I do not want to close the Transaction Scope before the second connection, since the confirmation of the transaction depends on the results of the second request to another database.

  • Make a second transaction in advance is impossible? - Monk
  • TransactionScope, like everything implicitly attached to a transaction ... Well, I, like, found a solution by creating TransactionScope.Supress. In theory, in this case, I should not use distributed transactions ... - iluxa1810
  • And create a second connection is not an option? - nzeemin
  • @nzeemin, this is how I do it ... The transaction starts in ORM, but then there is a need to turn to another database and there I create another connection and here is the error ... - iluxa1810

1 answer 1

Use transaction management manually:

using (var tran1 = conn1.BeginTransaction()) { // ... using (var tran2 = conn2.BeginTransaction()) { // ... tran2.Commit(); } // ... tran1.Commit(); } 

In EF Model First, you can manually start a EntityConnection through the EntityConnection :

 using (var conn = new EntityConnection("name=...")) using (var ctx = new MyContext(conn)) { conn.Open(); using (var tran = conn.BeginTransaction()) { // ... tran.Commit(); } } 

In EF Code First, manual transaction management goes through the Database :

 using (var tran = ctx.Database.BeginTransaction()) { // ... tran.Commit(); }