Ejemplo n.º 1
0
        public void TestLazyLoadBookAndReviewUsingProxiesPackageOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <LazyProxyContext>(
                builder => builder.UseLazyLoadingProxies());

            using (var context = new LazyProxyContext(options))
            {
                context.Database.EnsureCreated();
                var book = new BookLazyProxy
                {
                    Reviews = new List <LazyReview>
                    {
                        new LazyReview {
                            NumStars = 5
                        }, new LazyReview {
                            NumStars = 1
                        }
                    }
                };
                context.Add(book);
                context.SaveChanges();
            }
            using (var context = new LazyProxyContext(options))
            {
                //ATTEMPT
                var book = context.Books.Single();   //#A
                book.Reviews.Count().ShouldEqual(2); //#B

                /*********************************************************
                #A We just load the book class
                #B When the Reviews are read, then EF Core will read in the reviews
                * *******************************************************/
            }
        }
        public void TestLazyLoadCompareIncludeOk()
        {
            //SETUP
            var showLog = false;
            var options = SqliteInMemory.CreateOptionsWithLogging <LazyProxyContext>(log =>
            {
                if (showLog)
                {
                    _output.WriteLine(log.DecodeMessage());
                }
            }, builder: builder => builder.UseLazyLoadingProxies());

            using var context = new LazyProxyContext(options);
            context.Database.EnsureCreated();
            var book = new BookLazyProxy
            {
                Reviews = new List <LazyReview>
                {
                    new LazyReview {
                        NumStars = 5
                    }, new LazyReview {
                        NumStars = 1
                    }
                }
            };

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

            context.ChangeTracker.Clear();

            //ATTEMPT
            showLog = true;
            var book1 = context.Books.TagWith("lazy").Single();

            book1.Reviews.Count().ShouldEqual(2);
            var book2 = context.Books.TagWith("include").Include(x => x.Reviews).Single();

            book2.Reviews.Count().ShouldEqual(2);
        }