我正在将一些代码从Linq-to-sql移植到EF 4.1(代码优先),我有一堆测试通过调用相关方法测试我的Repository代码,检查记录是否已添加/删除按预期方式到数据库,然后对该事务进行回滚,以便在测试后恢复db状态。我可以在EF 4.1中这样做,如果是这样的话?
linq-to-sql等效示例如下所示:
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();
}