Пример #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();
                }
            }
        }
Пример #2
0
 public bool AddBorrowLog(BorrowLogContract borrowLog)
 {
     using (BookBorrowContext context = new BookBorrowContext())
     {
         context.BorrowLog.Add(borrowLog);
         int effect = context.SaveChanges();
         return effect == 1;
     }
 }
Пример #3
0
 public bool DeleteBook(long bookId)
 {
     using (BookBorrowContext context = new BookBorrowContext())
     {
         var book = context.Book.FirstOrDefault(t => t.Id == bookId);
         book.IsDel = true;
         int effect = context.SaveChanges();
         return effect == 1;
     }
 }
Пример #4
0
 public bool AddBook(BookContract book)
 {
     using (BookBorrowContext context = new BookBorrowContext())
     {
         book.IsDel = false;
         book.BookStatus = BookStatus.Avalable;
         context.Book.Add(book);
         int effect = context.SaveChanges();
         return effect == 1;
     }
 }
Пример #5
0
        public bool Rerturn(long bookId)
        {
            using (BookBorrowContext context = new BookBorrowContext())
            {
                //修改书本借阅状态为可借与修改归还时间两个操作必须都完成才有效
                using (var trans = context.Database.BeginTransaction())
                {
                    try
                    {
                        var book = context.Book.FirstOrDefault(t => t.Id == bookId);
                        //if(book == null)  ;
                        book.BookStatus = BookStatus.Avalable;

                        var borrowLog = context.BorrowLog.FirstOrDefault(t => t.BookId == bookId);
                        borrowLog.ReturnTime = DateTime.Now;

                        context.SaveChanges();
                        trans.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        //记录 ex
                        return false;
                    }
                    //using()会释放的,不必再 trans.Dispose();
                }
            }
        }