コード例 #1
0
        public static void WriteQueryInLog(string queryXml)
        {
            BookstoreEntities context = new BookstoreEntities();

            SearchLog newLog = new SearchLog()
            {
                Date     = DateTime.Now,
                QueryXml = queryXml
            };

            context.SearchLogs.Add(newLog);
            context.SaveChanges();
        }
コード例 #2
0
        public static void AddBook(string title, long?isbn, decimal?price, string webSite, List <string> authorNames, List <ReviewData> reviews)
        {
            TransactionScope tran = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions()
            {
                IsolationLevel = IsolationLevel.RepeatableRead
            });

            using (tran)
            {
                BookstoreEntities context = new BookstoreEntities();
                Book newBook = new Book()
                {
                    Title   = title,
                    ISBN    = isbn,
                    Price   = price,
                    Website = webSite
                };

                foreach (string authorName in authorNames)
                {
                    Author author = CreateOrLoadAuthor(context, authorName);
                    newBook.Authors.Add(author);
                }

                foreach (ReviewData reviewData in reviews)
                {
                    Review newReview = new Review()
                    {
                        CreationDate = reviewData.CreationDate,
                        Text         = reviewData.Text
                    };

                    if (reviewData.AuthorName != null)
                    {
                        Author author = CreateOrLoadAuthor(context, reviewData.AuthorName);
                        newReview.Author = author;
                    }

                    newBook.Reviews.Add(newReview);
                }

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

                tran.Complete();
            }
        }
コード例 #3
0
        private static Author CreateOrLoadAuthor(
            BookstoreEntities context, string authorName)
        {
            Author existAuthor =
                (from a in context.Authors
                 where a.Name == authorName
                 select a).FirstOrDefault();

            if (existAuthor != null)
            {
                return(existAuthor);
            }

            Author newAuthor = new Author();

            newAuthor.Name = authorName;
            context.Authors.Add(newAuthor);
            context.SaveChanges();

            return(newAuthor);
        }