private static void InsertData(XmlNodeList authors, string title, string webSite, string isbn, string priceStr, XmlNodeList reviews) { var context = new BookstoreEntities(); using (context) { Book book = new Book { Isbn = isbn, Title = title, Website = webSite, }; if (CheckForExistingBook(context, book)) { throw new ArgumentException("The book with isbn: {0} already exist!", isbn); } if (priceStr != null) { book.Price = decimal.Parse(priceStr); } if (authors != null) { foreach (XmlNode authorName in authors) { Author author = CreateOrLoadAuthor(context, authorName.InnerText); book.Authors.Add(author); } } if (reviews != null) { foreach (XmlNode reviewNode in reviews) { Review review = new Review(); review.CreationDate = DateTime.Now; if (reviewNode.Attributes["date"] != null) { string date = reviewNode.Attributes["date"].Value; review.CreationDate = DateTime.ParseExact(date, "d-MMM-yyyy", CultureInfo.InvariantCulture); } if (reviewNode.Attributes["author"] != null) { string authorStr = reviewNode.Attributes["author"].Value; Author author = CreateOrLoadAuthor(context, authorStr); review.AuthorId = author.AuthorId; } review.ReviewText = reviewNode.InnerText; book.Reviews.Add(review); } } context.Books.Add(book); context.SaveChanges(); } }
private static void InsertData(string authorName, string title, string webSite, string isbn, string priceStr) { var context = new BookstoreEntities(); Author author = CreateOrLoadAuthor(context, authorName); Book book; book = new Book { Isbn = isbn, Title = title, Website = webSite, }; if (priceStr != null ) { book.Price = decimal.Parse(priceStr); } book.Authors.Add(author); context.Books.Add(book); context.SaveChanges(); }
//TODO: FIX private static bool CheckForExistingBook(BookstoreEntities context, Book book) { if (context.Books.Any(x => x.Isbn == book.Isbn)) { return true; } return false; }