当我不需要做更多的事情时,我需要做回滚或其他事情吗?

I need to do rollback or somenthing when I don't need to do anything more?

我有这个方法 return 一个 bool:

using(Entities dbContext = new Entities())
{
    using (DbContextTransaction myTransaction = dbContext.Database.BeginTransaction())
    {
        //Get data from database

        //check data from database

        //if my data is the expected: return true;
            //???: rollback or something to do or is it enough with the return?


        //if not is expected:
            //update data;
            //dbContext.Savechages();
            //myTransaction.Commit();
            //return true;
    }
}

我的疑问是,如果我不需要对数据做更多的事情,是否足够 return 是真的还是建议进行回滚,或者在没有 SaveChanges 的情况下提交?

谢谢。

你应该在开始交易之前检查你的数据,当你从数据库读取数据时你不需要开始交易,因为你的逻辑的第一部分没有任何更新,插入......你不需要任何提交或回滚

其实你的代码应该是这样的

using(Entities dbContext = new Entities())
{
        //Get data from database

        //check data from database

        //if my data is the expected: return true;

using (DbContextTransaction myTransaction = dbContext.Database.BeginTransaction())
    {
         //if not is expected:
            try 
             {
               //update data;
               //dbContext.Savechages();
               //myTransaction.Commit();
               //return true;
             }
           catch(Exception ex)
           {
            transaction.Rollback();
            ......
            return false;
        }
    }
}