public void TestCreateOneEntryWithLogs()
        {
            //SETUP
            var showLog = false;
            var options = SqliteInMemory.CreateOptionsWithLogging <Chapter3DbContext>(log =>
            {
                if (showLog)
                {
                    _output.WriteLine(log.DecodeMessage());
                }
            });

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

                showLog = true;
                var itemToAdd = new ExampleEntity
                {
                    MyMessage = "Hello World"
                };

                //ATTEMPT
                context.SingleEntities.Add(
                    itemToAdd);
                context.SaveChanges();

                //VERIFY
                context.SingleEntities.Count().ShouldEqual(1);
            }
        }
        public void TestCreateOneEntry()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <Chapter3DbContext>();

            using (var context = new Chapter3DbContext(options))
            {
                context.Database.EnsureCreated();
                var itemToAdd = new ExampleEntity
                {
                    MyMessage = "Hello World"
                };

                //ATTEMPT
                context.Add(itemToAdd); //#A
                context.SaveChanges();  //#B

                /*********************************************************
                 #A It use the Add method to add the SingleEntity to the application's DbContext. The DbContext works what table to add it to based on its type of its parameter
                 #B It calls the SaveChanges() method from the application's DbContext to update the database
                 * ***********************************************************/

                //VERIFY
                context.SingleEntities.Count()
                .ShouldEqual(1);
                itemToAdd.ExampleEntityId
                .ShouldNotEqual(0);
            }
        }
示例#3
0
        public void TestAddReviewNoIncludeCollectionAlreadySetOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <Chapter3DbContext>();

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

            var writeBook = new BookCheckSet
            {
                Title   = "Title",
                Reviews = new List <ReviewSetCheck> {
                    new ReviewSetCheck {
                        NumStars = 5
                    }
                }
            };

            context.Add(writeBook);
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //ATTEMPT
            var book = context.BookCheckSets
                       .Single(p => p.BookId == writeBook.BookId);

            book.Reviews.Add(new ReviewSetCheck {
                NumStars = 5
            });
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //VERIFY
            var bookAgain = context.BookCheckSets
                            .Include(p => p.Reviews)
                            .Single(p => p.BookId == book.BookId);

            bookAgain.Reviews.ShouldNotBeNull();
            bookAgain.Reviews.Count.ShouldEqual(2);
        }