示例#1
0
        public void GetAllBooks1()
        {
            var books = new Database("product").ExecuteReader("select * from Book", null, dr => {
                var book = new Book {Id = (int) dr["Id"], Name = (string) dr["Name"]};
                return book;
            });

            foreach (var book in books)
                Console.WriteLine(book.Name);
        }
示例#2
0
        private static Book AddBook(string title, string description, DateTime datePublished, int pages, string isbn, int bookCopies)
        {
            using (BookDBDataContext context = new BookDBDataContext())
            {
                Book newBook = new Book()
                {
                    Title = title,
                    BookResume = description,
                    DatePublished = datePublished,
                    Pages = pages,
                    ISBN = isbn
                };

                context.Books.InsertOnSubmit(newBook);

                while (bookCopies > 0)
                {
                    var bookCopy = new BookCopy()
                    {
                        Book = newBook
                    };
                    context.BookCopies.InsertOnSubmit(bookCopy);
                    bookCopies--;
                }

                //var existing = context.Authors.FirstOrDefault(a => a.FirstName.Contains("G"));

                //if (existing != null)
                //{
                //    var bookAuthor = new BookAuthor()
                //    {
                //        BookID = newBook.ID,
                //        AuthorID = existing.ID
                //    };
                //}

                //var bookGenre = new BookGenre()
                //{
                //    BookID = newBook.ID
                //};

                context.SubmitChanges();
                return newBook;
            }
        }
示例#3
0
        public static void LoanBookToUser(User user, Book book)
        {
            using (var context = new BookDBDataContext())
            {
                bool alreadyLoaned = (from bu in context.BookUsers
                                  where bu.UserID == user.ID
                                    && bu.BookCopy.Book.ID == book.ID
                                    && bu.ReturnDate >= DateTime.Now.Date
                                  select 1).Count() > 0;

                if (alreadyLoaned == true)
                {
                    throw new ArgumentException("You have already loaned this book!");
                }

                BookCopy availableCopy = (from bu in context.BookUsers
                                          where bu.BookCopy.BookID == book.ID
                                          && bu.ReturnDate == null
                                          select bu.BookCopy).FirstOrDefault();

                var avail = (from bc in context.BookCopies
                             where bc.BookID == book.ID
                                 && bc.BookUsers.Count() == 0
                             select bc).FirstOrDefault();

                if (availableCopy == null)
                {
                    throw new ArgumentException("There is no available copy of this book!");
                }

            }
        }
示例#4
0
 partial void DeleteBook(Book instance);
示例#5
0
 partial void UpdateBook(Book instance);
示例#6
0
 partial void InsertBook(Book instance);