This is a migrated thread and some comments may be shown as answers.

OpenAccess and TransactionScope

3 Answers 128 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joshua
Top achievements
Rank 1
Joshua asked on 15 Oct 2012, 10:33 PM
It does not appear that OA plays well with TransactionScope.  My OA version is 2012.2.816.1.  Here is the use case that consistently fails:

1. Create an integration test and surround the code inside the integration test with a using(TransactionScope... block.
2. Create a new context, add an item to the context, and call the SaveChanges.  Note I am NOT calling transaction.Complete() because when it leaves the block, I want the change rolled back.
3. Load that same item from the context
4. The item was not actually saved to the database -- you can tell because the ID field is still 0.

Here is a sample code block:

using (TransactionScope transaction = new TransactionScope())
{
 using(MyDbContext context = new MyDbContext())
  {
   var item = new Item();
   item.Name = "some random new name";
   context.Add(item);
   context.SaveChanges();
 
   // the item.Id property is still 0, even though it should have been saved, so the below will fail
   Assert.AreNotEqual(0, item.Id);
 
  }
}


This is causing significant headaches for our integration testing.  Two questions:

1. What is the support of OA with Transaction Scope?
2. How can an automatic rollback of changes be implemented, if TS is not supported?  In order to accurately test code, I need changes to be saved to the DB, tests cases run, and then rolled back between tests.  I'm not wild about running a script between each test, when TS typically has done this for me. 

Thanks!

3 Answers, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 18 Oct 2012, 10:54 AM
Hi Joshua,

 A call to context.SaveChanges() will just enlist the context in the current system transaction. OpenAccess will only attempt to commit the changes to the database if you commit the system transaction by calling transaction.Complete().

If your test actually needs the data to be committed you should ideally call context.SaveChanges() without the necessity of system transactions. Rolling back the transaction without committing it is not really testing whether the data was successfully (and durably) saved.
 You can then cleanup the test data in a method marked with the [TestInitialize] or [TestCleanup] attribute (in case you are using the Microsoft Visual Studio test framework) which is executed before/after each test. Here you can delete whatever data you need to via the context. I prefer before the test so that the data is available for post test analysis.

Do get back in case you need further assistance.

Kind regards,
Ady
the Telerik team
Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
0
Justin
Top achievements
Rank 1
answered on 08 Mar 2013, 09:52 PM
"you should ideally call context.SaveChanges() without the necessity of system transactions"  This does not apply to me.

I have code in a TransactionScope that uses the ID generated from the first insert to insert related records.  
That ID has to be returned and I shouldn't have to wait until the TransactionScope commits.

How do I have OpenAccess do what it should do.  Insert the record, let SQL generate a Identity, and let the Transactional Database handle the rollback if I don't commit the transaction scope?  

Thanks,
Justin
0
Ralph Waldenmaier
Telerik team
answered on 13 Mar 2013, 03:56 PM
Hello Justin,

In order to get the appropriate Id's that are generated by the server, you can use the FlushChanges() method on the Context. This will flush the data to the server and calculates the primary keys immediately. Also please add a call to SaveChanges() afterwards. This will ensure that the transaction is enlisted in your transaction scope.

I hope this information is helpful for you.
Feel free to ask in case you have any other question 

Greetings,
Ralph
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
Tags
Development (API, general questions)
Asked by
Joshua
Top achievements
Rank 1
Answers by
Ady
Telerik team
Justin
Top achievements
Rank 1
Ralph Waldenmaier
Telerik team
Share this question
or