Ejemplo n.º 1
0
        /// <summary>
        /// 修改书本借阅状态为不可借、增加借阅记录
        /// 使用事务
        /// </summary>
        /// <param name="borrowLog"></param>
        /// <returns></returns>
        public bool Borrow(BorrowLogContract borrowLog)
        {
            using (BookBorrowContext context = new BookBorrowContext())
            {
                //修改书本借阅状态为不可借与增加借阅记录两个操作必须都完成才有效
                using (var trans = context.Database.BeginTransaction())
                {
                    try
                    {
                        var book = context.Book.FirstOrDefault(t => t.Id == borrowLog.BookId);
                        //if(book == null)  ;
                        book.BookStatus = BookStatus.UnAvalable;

                        borrowLog.ReturnTime = new DateTime(1900, 1, 1);
                        context.BorrowLog.Add(borrowLog);

                        context.SaveChanges();
                        trans.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        //记录 ex
                        return false;
                    }
                    //using()会释放的,不必再 trans.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
 public bool AddBorrowLog(BorrowLogContract borrowLog)
 {
     using (BookBorrowContext context = new BookBorrowContext())
     {
         context.BorrowLog.Add(borrowLog);
         int effect = context.SaveChanges();
         return effect == 1;
     }
 }
Ejemplo n.º 3
0
 public ActionResult Borrow(BorrowLogContract borrowLog)
 {
     service.Borrow(borrowLog);
     return RedirectToAction("Index");
 }
Ejemplo n.º 4
0
 public ActionResult Borrow(long bookId)
 {
     BorrowLogContract borrowLog = new BorrowLogContract {BookId = bookId, BorrowTime = DateTime.Now};
     return View(borrowLog);
 }