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; } }