When trying to use DbContext that uses connection that has already been opened in the TransactionScope, we get exception:
MSDTC on server 'ServerName' is unavailable.
Same scenario works when using ObjectContext or a closed connection. Also, the scenario works if we disable DatabaseInitializer. Scenario also works if the connection already contains the "Application Name=EntityFrameworkMUE" cookie. Apparently we mess up with the connection during database initialization, which causes promotion to a distributed transaction and hence the error.
Here is the repro:
var connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=TransactionET;Integrated Security=SSPI;MultipleActiveResultSets=True;";
var sqlConnection = new SqlConnection(connectionString);
using (var ts = new TransactionScope())
{
sqlConnection.Open();
using (var ctx = new TransactionContext(sqlConnection, contextOwnsConnection: false))
{
var blog = new Blog
{
Title = "My Blog",
};
ctx.Blogs.Add(blog);
ctx.SaveChanges();
}
}
Comments: Note for the dev: I added a couple of tests in: \entityframework\test\EntityFramework\FunctionalTests\Objects\TransactionsTests.cs for that bug. These tests are currently commented - please uncomment them after the bug has been fixed.
MSDTC on server 'ServerName' is unavailable.
Same scenario works when using ObjectContext or a closed connection. Also, the scenario works if we disable DatabaseInitializer. Scenario also works if the connection already contains the "Application Name=EntityFrameworkMUE" cookie. Apparently we mess up with the connection during database initialization, which causes promotion to a distributed transaction and hence the error.
Here is the repro:
var connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=TransactionET;Integrated Security=SSPI;MultipleActiveResultSets=True;";
var sqlConnection = new SqlConnection(connectionString);
using (var ts = new TransactionScope())
{
sqlConnection.Open();
using (var ctx = new TransactionContext(sqlConnection, contextOwnsConnection: false))
{
var blog = new Blog
{
Title = "My Blog",
};
ctx.Blogs.Add(blog);
ctx.SaveChanges();
}
}
Comments: Note for the dev: I added a couple of tests in: \entityframework\test\EntityFramework\FunctionalTests\Objects\TransactionsTests.cs for that bug. These tests are currently commented - please uncomment them after the bug has been fixed.