Example #1
0
        public void TestDeletePrincipal()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <Chapter06Context>();

            using var context = new Chapter06Context(options);
            context.Database.EnsureCreated();
            context.Add(new OnePrincipal {
                Link = new OneDependent()
            });
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //ATTEMPT
            var depToRemove = new OnePrincipal {
                Id = 1
            };

            context.Remove(depToRemove);
            context.SaveChanges();

            //VERIFY
            context.OneDependents.Count().ShouldEqual(0);
            context.OnePrincipals.Count().ShouldEqual(0);
        }
        public void TestCostOfNotDetaching(int numCollection)
        {
            //SETUP
            ManyTop entityToDetach;
            var     options = SqliteInMemory.CreateOptions <Chapter06Context>();

            using (var context = new Chapter06Context(options))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                using (new TimeThings(_output, "SaveChanges - no tracked entities."))
                {
                    context.SaveChanges();
                }
                using (new TimeThings(_output, "SaveChanges - no tracked entities."))
                {
                    context.SaveChanges();
                }

                entityToDetach = context.AddManyTopWithRelationsToDb(numCollection);
                var numEntities = entityToDetach.Collection1.Count +
                                  entityToDetach.Collection2.Count +
                                  entityToDetach.Collection3.Count;


                using (new TimeThings(_output, $"SaveChanges - {numEntities:n0} tracked entities."))
                {
                    context.SaveChanges();
                }
                using (new TimeThings(_output, $"SaveChanges - {numEntities:n0} tracked entities."))
                {
                    context.SaveChanges();
                }
            }

            //VERIFY
        }
        public void TestMissingIncludeNotSafeOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <Chapter06Context>();

            using var context = new Chapter06Context(options);
            context.Database.EnsureCreated();
            var bookSetup = new BookNotSafe();

            bookSetup.Reviews.Add(new ReviewNotSafe());
            context.Add(bookSetup);
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //ATTEMPT
            var book = context.Books //#A
                                     //... missing Include(x => x.Reviews) //#B
                       .First(x => x.Reviews.Any());

            //VERIFY
            book.Reviews.ShouldNotBeNull();
        }