public void TestTransaction() { using (var scope = new EntityTransactionScope()) using (var context = new DbContext()) { TestAnyMethod(context.Database); scope.Complete(); } }
/// <summary> /// 检查是否需要开启事务。 /// </summary> /// <param name="info"></param> private void CheckTransaction(InterceptCallInfo info) { //判断是否加了 TransactionSupportAttribute 特性 if (info.Member.IsDefined <TransactionSupportAttribute>()) { //判断是否已经开启事务 if (EntityTransactionScope.Current == null) { transaction = new EntityTransactionScope(); } } }
public void BatchCreateTest() { using (var scope = new EntityTransactionScope()) using (var per = new EntityPersister <Categories>(instanceName)) { var products = new List <Categories>(); for (var i = 0; i < 1000; i++) { products.Add(new Categories { CategoryID = i + 1, CategoryName = "测试" + i, Description = "aa" }); } per.BatchCreate(products); scope.Complete(); } }
public void Intercept(InterceptCallInfo info) { if (info.InterceptType == InterceptType.BeforeMethodCall) { CheckTransaction(info); } //执行方法后 else if (info.InterceptType == InterceptType.AfterMethodCall) { CommitTransaction(info); } //最终完成后 else if (info.InterceptType == InterceptType.Finally) { if (transaction != null) { transaction.Dispose(); transaction = null; } } }