asp.net mvc 事务处理:Transactions


1.在控制器里引用using System.Transactions;

2.在你需要事务回滚的地方外面套一层using (TransactionScope sc = new TransactionScope()){}

using (TransactionScope sc = new TransactionScope())
            {
                try
                {
                    using (wcfDemoEntities db = new wcfDemoEntities())
                    {
                        user user2 = new user() {  UserName = name, PassWord = "1212",Discribe="fdsf",SubmitTime=DateTime.Now };
                        db.user.Add(user2);
                        db.SaveChanges();
                    }
                    sc.Complete();//放在catch上面,否则不能回滚
                }
                catch (Exception)
                {
                }
            }
            return "";

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM