How to rollback in EF4?

Posted by Jaxidian on Stack Overflow See other posts from Stack Overflow or by Jaxidian
Published on 2010-04-21T20:50:09Z Indexed on 2010/04/21 20:53 UTC
Read the original article Hit count: 812

In my research about rolling back transactions in EF4, it seems everybody refers to this blog post or offers a similar explanation. In my scenario, I'm wanting to do this in a unit testing scenario where I want to rollback practically everything I do within my unit testing context to keep from updating the data in the database (yeah, we'll increment counters but that's okay). In order to do this, is it best to follow the following plan? Am I missing some concept or anything else major with this (aside from my SetupMyTest and PerformMyTest functions won't really exist that way)?

using (var ts = new TransactionScope())
{
  // Arrange
  SetupMyTest(context);

  // Act
  PerformMyTest(context);
  var numberOfChanges = context.SaveChanges(false);
  // if there's an issue, chances are that an exception has been thrown by now.

  // Assert
  Assert.IsTrue(numberOfChanges > 0, "Failed to _____");

  // transaction will rollback because we do not ever call Complete on it
}

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about entity-framework-4