.Net MVC架构实现事务回滚处理

1.首先我们需要 添加引用:using System.Transactions;

2.实现事务回滚

注:事务回滚的要用using (TransactionScope sc = new TransactionScope()){}

using System.Transactions;

public ActionResult Index()
        {

            using (TransactionScope sc = new TransactionScope())
            {
                try
                {
                    using (DBEntities db = new DBEntities())
                    {

                        WorkTime wt = new WorkTime() { DayTime=DateTime.Now,LongTime=12 };
                        db.WorkTime.Add(wt);
                       
                        db.SaveChanges();
                    }
                    sc.Complete();//一定要放在catch上面,否则不能回滚
                }
                catch (Exception)
                {
                    
                }
            }

            return View();
        }

猜你喜欢

转载自blog.csdn.net/qq_39585172/article/details/81095264