コード例 #1
0
        public void AddBookToAuthor(int authorId, Book book)
        {
            using (Context)
            {
                try
                {
                    var author = Context.Authors.Find(authorId);

                    if (author.Books == null)
                        author.Books = new List<Book>();

                    author.Books.Add(book);

                    var bookToUpdate = Context.Books.Find(book.Id);

                    if (bookToUpdate.Authors == null)
                        bookToUpdate.Authors = new List<Author>();

                    bookToUpdate.Authors.Add(author);
                    Context.SaveChanges();
                }
                catch
                {

                    throw;
                }
            }
        }
コード例 #2
0
ファイル: BookRepository.cs プロジェクト: vault60/WebLibrary
 public void AddBook(Book book)
 {
     try
     {
         Context.Books.Add(book);
         Context.SaveChanges();
     }
     catch
     {
         throw;
     }
 }
コード例 #3
0
ファイル: BookController.cs プロジェクト: vault60/WebLibrary
        public ActionResult Create(string title, int quantity, int authorId)
        {
            if (title.Length < 1 || quantity < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            var authorRepo = new AuthorRepository();
            var bookToAdd = new Book();

            bookToAdd.Title = title;
            bookToAdd.Quantity = quantity;

            var bookRepo = new BookRepository();
            bookRepo.AddBook(bookToAdd);

            authorRepo.AddBookToAuthor(authorId, bookToAdd);

            return RedirectToAction("Create");
        }