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; } } }
public void AddBook(Book book) { try { Context.Books.Add(book); Context.SaveChanges(); } catch { throw; } }
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"); }