EF 事务处理 (InnoDB Engine的MySQL表也可以)

备忘

1. 亲测(可以嵌套使用)

using (TransactionScope scope = new TransactionScope())
{
      //操作1
      XXEntities.Current.SaveChanges();

      //操作2
      XXEntities.Current.SaveChanges();
      
      //其他Context操作
      XXEntities2.Current.SaveChanges();

      scope.Complete();
      BoshccEntities.Current.AcceptAllChanges();  
}

 2.

XXEntities.Current.Connection.Open();
using (var tran = XXEntities.Current.Connection.BeginTransaction())
{
      try
      {
            //....
            XXEntities.Current.SaveChanges();
            tran.Commit();
      }
      catch (Exception ex)
      {
            tran.Rollback();
            throw ex;
      }
      finally
      {
            XXEntities.Current.Connection.Close();
            tran.Dispose();
      }
}

 

posted @ 2014-09-19 11:32  xachary  阅读(384)  评论(0编辑  收藏  举报