Esempio n. 1
0
        public void TestLazyLoadBookAndReviewUsingILazyLoaderOk()
        {
            //SETUP
            var showlog = false;
            var options = SqliteInMemory.CreateOptionsWithLogging <LazyInjectContext>(log =>
            {
                if (showlog)
                {
                    _output.WriteLine(log.Message);
                }
            });

            using (var context = new LazyInjectContext(options))
            {
                context.Database.EnsureCreated();
                var book = new BookLazy1
                {
                    Promotion = new PriceOffer {
                        NewPrice = 5
                    },
                    Reviews = new List <Lazy1Review>
                    {
                        new Lazy1Review {
                            NumStars = 5
                        }, new Lazy1Review {
                            NumStars = 1
                        }
                    }
                };
                context.Add(book);
                context.SaveChanges();
            }
            using (var context = new LazyInjectContext(options))
            {
                //ATTEMPT
                showlog = true;
                var book    = context.BookLazy1s.Single(); //#A
                var reviews = book.Reviews.ToList();       //#B

                //VERIFY
                book.Promotion.ShouldBeNull();
                context.BookLazy1s.Select(x => x.Promotion).ShouldNotBeNull();
                reviews.Count.ShouldEqual(2);
            }
        }
        public void TestLazyLoadBookAndReviewUsingILazyLoaderOk()
        {
            //SETUP
            var showlog = false;
            var options = SqliteInMemory.CreateOptionsWithLogging <Lazy1DbContext>(log =>
            {
                if (showlog)
                {
                    _output.WriteLine(log.Message);
                }
            });

            using (var context = new Lazy1DbContext(options))
            {
                context.Database.EnsureCreated();
                var book = new BookLazy1
                {
                    Reviews = new List <LazyReview>
                    {
                        new LazyReview {
                            NumStars = 5
                        }, new LazyReview {
                            NumStars = 1
                        }
                    }
                };
                context.Add(book);
                context.SaveChanges();
            }
            using (var context = new Lazy1DbContext(options))
            {
                //ATTEMPT
                showlog = true;
                var book    = context.BookLazy1s.Single(); //#A
                var reviews = book.Reviews.ToList();       //#B

                /*********************************************************
                #A This gets an instance of the BookLazy entity class that has configured its Reviews property to use lazy loading
                #B When the Reviews property is accessed, then EF Core will read in the reviews from the database
                * *******************************************************/

                //VERIFY
                reviews.Count.ShouldEqual(2);
            }
        }