I know it looks like a duplicated since there are tons of questions regarding transactions on multiple contexts but none of them refer to this scenarios.
When we do something like:
var contextA = new ContextA();
var contextB = new ContextB();
using(var scope = new TransactionScope())
{
var objA = new EntityA();
objA.Name = "object a";
contextA.EntitiesA.Add(objA);
contextA.SaveChanges();
var objB = new EntityB();
objB.Name = "object B";
contextB.EntitiesB.Add(objB);
contextB.SaveChanges();
scope.Complete();
}
A System.Data.Entity.Core.EntityException
is thrown on the call of contextA.SaveChanges()
with the following messages:
Root Exception: The underlying provider failed on EnlistTransaction.
Inner Exception: Connection currently has transaction enlisted. Finish current transaction and retry.
So, anyone have a clue on what exactly is going wrong with this sample?
We are trying to have a single transaction using multiple contexts and each context with its own connection to the database. Obviously since the data of each context is on different database servers(in production) we can't use the DbContext ctor that receives a DbConnection and shares it with both contexts, so share a DbConnection is not an option.
Thank you very much, I really appreciate any help.
Distributed transactions with TransactionScope are now supported by Azure SQL Database. See TransactionScope() in Sql Azure.