?????????????????????????. ???????????????????. ???????????????????????????????β??????????? ???????????????????????.
?????????????????Щ??????????????????. ?????????????????????????????????. ????????????????create?? read?? update??delete(???CRUD)
?????? ??????????????????Add?????????????????????? ????????????????????. ???????? ??????????????????????????????????.
???????????????????.net 2.0??????TransactionScope?? ?????????????MsTest????????:
1:      [TestClass]
2:      public class DbTestBase
3:      {
4:          private TransactionScope scope;
5:
6:          [TestInitialize]
7:          public void SetUp()
8:          {
9:              this.scope = new TransactionScope();
10:          }
11:
12:          [TestCleanup]
13:          public void TearDown()
14:          {
15:              this.scope.Dispose();
16:          }
17:      }
??????????????????????TestInitialize????SetUp?????д???TransactionScope?????????TestCleanup????TearDown?????е???TransactionScope??Dispose????.  ???????????????????:
1:      [TestClass]
2:      public class DateBaseTesting : DbTestBase
3:      {
4:          /// <summary>
5:          /// Test Insert record to database
6:          /// </summary>
7:          /// <seealso cref="http://www.cnblogs.com/wintersun"/>
8:          /// <remarks>Any database modification will be roll back</remarks>
9:          [TestMethod]
10:          public void TestAddWithEmployeeRepository()
11:          {
12:              //arrange
13:              var employee = this.CreateNewEmployee();
14:              var employRepository = RepositoryHelper.GetEmployeeRepository();
15:
16:              //act
17:              employRepository.Add(employee);
18:              employRepository.Save();
19:
20:              //assert
21:              var employeelist =
22:                 employRepository.Repository.Find(e => e.EmployeeID == employee.EmployeeID);
23:              Assert.IsNotNull(employeelist);
24:              CollectionAssert.AreEqual(new List<Employee>() { employee }?? employeelist.ToList());
25:          }
26:
27:
28:          private Employee CreateNewEmployee()
29:          {
30:              var employee = new Employee
31:              {
32:                  ManagerID = 2??
33:                  ContactID = 3??
34:                  Title = "Developer"??
35:                  BirthDate = new DateTime(1965?? 1?? 1?? 0?? 0?? 0)??
36:                  HireDate = DateTime.Now??
37:                  Gender = "M"??
38:                  MaritalStatus = "M"??
39:                  ModifiedDate = DateTime.Now??
40:                  NationalIDNumber = "2"??
41:                  rowguid = new Guid()??
42:                  CurrentFlag = true??
43:                  VacationHours = 2??
44:                  SickLeaveHours = 3??
45:                  SalariedFlag = false??
46:                  LoginID = "myworkbase\peter"
47:              };
48:              return employee;
49:          }
50:
51:      }
?????????TestAddWithEmployeeRepository?г????????????????EntityFramework??Repository???? ???????????????????壬???????.  ????п??????????κδ???飬ADO.NET??????????????????. ???????????????????????TransactionScope???????. ?????????????????????.
????????????????? ??????????????У? ??????????:
1:          [TestMethod]
2:          public void TestWrapTransactionScope()
3:          {
4:              WrapTransactionScope(() => TestAddWithEmployeeRepository());
5:          }
6:
7:          /// <summary>
8:          /// Wraps the transaction scope for unit testing
9:          /// </summary>
10:          /// <param name="action">The action method</param>
11:          /// <remarks>author http://www.cnblogs.com/wintersun </remarks>
12:          public void WrapTransactionScope(Action action)
13:          {
14:              using (var scope = new TransactionScope())
15:              {
16:                  action();
17:              }
18:          }