コード例 #1
0
        public static void AddBookInDB(string author, string title, string isbn, string price, string site)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            BookstoreDBEntities context = new BookstoreDBEntities();
            using (context)
            {
                Book book = new Book();
                if (isbn != null)
                {
                    book.ISBN = long.Parse(isbn);
                }
                else
                {
                    book.ISBN = null;
                }

                if (price != null)
                {
                    book.Price = decimal.Parse(price);
                }
                else
                {
                    book.Price = null;
                }

                book.WebSite = site;


                book.Title = title.ToLower();

                context.Books.Add(book);
                context.SaveChanges();

                // is author in the db
                var existingAuthor =
                (from a in context.Authors
                 where a.Name == author.ToLower()
                 select a).FirstOrDefault();

                if (existingAuthor == null)
                {
                    Author newAuthor = new Author()
                    {
                        Name = author.ToLower(),
                    };

                    context.Authors.Add(newAuthor);
                    context.SaveChanges();

                    existingAuthor = newAuthor;
                }

                book.Authors.Add(existingAuthor);
                context.SaveChanges();
            }
        }
コード例 #2
0
        public static void AddComplexInDB(List<string[]> author, string title, string isbn, string price, string site,
            List<string[]> reviews)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            BookstoreDBEntities context = new BookstoreDBEntities();

            using (context)
            {
                List<Author> allAuthors = AddAuthors(context, author);
                List<Review> allReviews = AddReviews(context, reviews);

                Book book = new Book();
                if (isbn != null)
                {
                    book.ISBN = long.Parse(isbn);
                }
                else
                {
                    book.ISBN = null;
                }

                if (price != null)
                {
                    book.Price = decimal.Parse(price);
                }
                else
                {
                    book.Price = null;
                }

                book.WebSite = site;

                book.Title = title.ToLower();

                context.Books.Add(book);
                context.SaveChanges();

                // is author in the db

                foreach (var a in allAuthors)
                {
                    book.Authors.Add(a);
                    context.SaveChanges();
                }

                foreach (var r in allReviews)
                {
                    book.Reviews.Add(r);
                    context.Reviews.Add(r);
                    context.SaveChanges();
                }
            }
        }