Ejemplo n.º 1
0
        private static Author CreateOrLoadAuthor(BookstoreEntities context, string authorName)
        {
            var user = context.Authors.FirstOrDefault(x => x.Name == authorName);
            if (user != null)
            {
                return user;
            }

            var newAuthor = new Author
            {
                Name = authorName
            };

            context.Authors.Add(newAuthor);
            context.SaveChanges();
            return newAuthor;
        }
Ejemplo n.º 2
0
        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();
        }
        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();
            }
        }