//罚款业务 public void ProcessFine(List <Book> books) { //db是一个具有各项模型属性的上下文类 using (var db = new 校园自助图书管理系统Entities()) { //创建事务 using (var dbContextTransaction = db.Database.BeginTransaction()) { try { foreach (var book in books) { Fine fine = new Fine(); fine.FinePrice = 0; db.Fine.Add(fine); } db.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); } } } }
//缴纳罚款业务 /* * 根据Id找到数据库中对应的Fine数据行,修改其FinePrice */ public void ProcessPayFine(Reader reader, int Id) { using (var db = new 校园自助图书管理系统Entities()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { Reader re = db.Reader.FirstOrDefault(r => r.Id == reader.Id); Fine fine = db.Fine.FirstOrDefault(f => f.Id == Id); Borrow br = db.Borrow.FirstOrDefault(b => b.FineId == Id); re.Price -= fine.FinePrice; fine.FinePrice = 0; br.NeedtoFine = "已缴清"; db.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); } } } }