Example #1
0
 public void TestAddBooks()
 {
     using (var context = new MyDBContext())
     {
         List <Book> books = OneToManyHelper.CreateBooks(10, 10);
         context.Books.AddRange(books);
         int result = context.SaveChanges();
         Assert.IsTrue(result > 0);
     }
 }
Example #2
0
        public void TestAddBookReviewToBooks()
        {
            using (var context = new MyDBContext())
            {
                Book       firstBook   = context.Books.FirstOrDefault();
                int        reviewCount = firstBook.BookReviews.Count;
                BookReview review      = OneToManyHelper.CreateBookReview(firstBook);
                int        result      = context.SaveChanges();
                Assert.IsTrue(result > 0);

                //重新加载图书
                Book firstBookFromDB = context.Books.FirstOrDefault();
                //书评数加一
                Assert.IsTrue(firstBookFromDB.BookReviews.Count == reviewCount + 1);
                //应该能找得到新加的书评
                BookReview newReview = firstBookFromDB.BookReviews.First(br => br.BookReviewId == review.BookReviewId);
                Assert.IsNotNull(newReview);
            }
        }
Example #3
0
 public void TestBookReviewMove()
 {
     using (var context = new MyDBContext())
     {
         //找到第一本和第二本书,把第一本书的第一条书评移给第二本书
         Book firstBook  = context.Books.Include("BookReviews").FirstOrDefault();
         Book secondBook = context.Books.Include("BookReviews").ToList().ElementAt(1);
         if (firstBook != null && firstBook.BookReviews.Count > 0)
         {
             BookReview bookReview = firstBook.BookReviews.ElementAt(0);
             OneToManyHelper.ShowBookReivew(bookReview);
             //先从原对象集合中移除
             firstBook.BookReviews.Remove(bookReview);
             //再追加到新对象的集合中
             secondBook.BookReviews.Add(bookReview);
             int result = context.SaveChanges();
             Assert.IsTrue(result > 0);
             //重新提取书评
             BookReview reviewFromDB = context.BookReviews.FirstOrDefault(r => r.BookReviewId == bookReview.BookReviewId);
             Assert.IsTrue(reviewFromDB.BookId == secondBook.BookId);
             OneToManyHelper.ShowBookReivew(reviewFromDB);
         }
     }
 }