c#事务

using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    db.Entry<Classroom_Info>(entity);
                    db.Set<Classroom_Info>().Add(entity);

                    //绑定学生
                    entity.User_Classroom_R.ToList().ForEach(i =>
                    {
                        i.ClassroomInfoID = maxID + 1;
                        db.Entry<User_Classroom_R>(i);
                        db.Set<User_Classroom_R>().Add(i);
                    });
                    db.SaveChanges();//是否为msdtc就看它提交的次数
                    trans.Complete();
                }
                catch (Exception)
                {
                    trans.Dispose();//出现异常,事务手动释放
                    throw;
                }
            }
posted @ 2016-01-19 11:58  chenjingchun  阅读(165)  评论(0编辑  收藏  举报