Code: Select all
[TestMethod()]
public void MicrosoftTransactieScenario4Test()
{
ClearTable();
using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
{
EntityConnection connection = new EntityConnection("name=TestModelContainer");
TestModelContainer ent = new TestModelContainer(connection);
var query = from t in ent.TEST_ALACOU_BAR_TPB_APP
where t.APP_CODE == "MYAPP"
select t;
Assert.IsTrue(query.Count() == 0);
// Create
TEST_ALACOU_BAR_TPB_APP testEnt = new TEST_ALACOU_BAR_TPB_APP();
testEnt.APP_ID = 1;
testEnt.APP_CODE = "MYAPP";
testEnt.ENVIRONMENT = "DEV";
testEnt.APP_NAME = "MyOnlyApp";
testEnt.APP_TYPE = "GUI";
ent.AddToTEST_ALACOU_BAR_TPB_APP(testEnt);
ent.SaveChanges();
// Read
Assert.IsTrue(query.Count() == 1);
// Update
testEnt.APP_NAME = "MyOnlyApp2";
ent.SaveChanges();
using (System.Transactions.TransactionScope ts2 = new System.Transactions.TransactionScope()) // System.Transactions.TransactionScopeOption.RequiresNew
{
Assert.IsTrue(query.Count() == 1);
Assert.IsTrue(query.First().APP_NAME == "MyOnlyApp2");
// Update
testEnt.APP_NAME = "MyOnlyApp3333";
ent.SaveChanges();
Assert.IsTrue(query.First().APP_NAME == "MyOnlyApp3333");
}
// Read
Assert.IsTrue(query.Count() == 1); // the test fails on this line...
Assert.IsTrue(query.First().APP_NAME == "MyOnlyApp2");
ts.Complete();
}
}
Error message:
Test method Domain.BasisArchitectuur.TestEf.BarObjectContextTest.MicrosoftTransactieScenario4Test threw exception:
System.Data.EntityException: The underlying provider failed on Open. ---> System.Transactions.TransactionException: The operation is not valid for the state of the transaction.
Error stack:
System.Transactions.TransactionState.EnlistVolatile(InternalTransaction tx, IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions, Transaction atomicTransaction)
System.Transactions.Transaction.EnlistVolatile(IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions)
Devart.Common.DbConnectionInternal.EnlistToDistributedTransactionInternal(Transaction transaction)
Devart.Common.DbConnectionInternal.c()
Devart.Common.DbConnectionFactory.a(DbConnectionBase A_0)
Devart.Common.DbConnectionClosed.Open(DbConnectionBase outerConnection)
Devart.Common.DbConnectionBase.Open()
Devart.Data.Oracle.OracleConnection.Open()
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
System.Data.EntityClient.EntityConnection.Open()
System.Data.Objects.ObjectContext.EnsureConnection()
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
System.Data.Objects.ELinq.ObjectQueryProvider.b__3[TResult](IEnumerable`1 sequence)
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
System.Linq.Queryable.Count[TSource](IQueryable`1 source)
TestEf.BarObjectContextTest.MicrosoftTransactieScenario4Test() in C:\Projects\TestEf\ObjectContextTest.cs: line 713
It looks like the connection was closed after leaving the inner scope. I was hoping that the connection and transaction would stay open on the databaselevel (using savepoints for the innertransactions).
Any suggestions?