3

I'm porting some code from Linq-to-sql to EF 4.1 (Code first), and I have a bunch of tests that test my Repository code by calling the method in question, checking that the record is added/deleted to the database as expected, and then does a roll-back on that transaction so that the db state is restored after the test. Can I do this in EF 4.1, and if so how?

An example linq-to-sql equivalent looks like this:

try
{
    context.Connection.Open();
    context.Transaction = context.Connection.BeginTransaction();
    _userRole.Context = context;

    _userRepo.SaveUser(new User { CreationDate = DateTime.Now, EmailAddress = "a@a.com", Password = "pwd", UserName = "unittest" });

    var user = _userRepo.GetUser("unittest");
    Assert.That(user.UserName, Is.EqualTo("unittest"));
}
finally
{
    context.Transaction.Rollback();
}
Matt Roberts
  • 26,371
  • 31
  • 103
  • 180

0 Answers0