public void TestRightWayToSetupRelationshipsOk()
        {
            //SETUP
            var showlog = false;
            var options = this.CreateUniqueClassOptionsWithLogging <EfCoreContext>(log =>
            {
                if (showlog)
                {
                    _output.WriteLine(log.Message);
                }
            });

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureClean();

                showlog = true;
                //ATTEMPT
                var book = new Book                  //#A
                {                                    //#A
                    Title   = "Test",                //#A
                    Reviews = new List <Review>()    //#A
                };                                   //#A
                book.Reviews.Add(                    //#B
                    new Review {
                    NumStars = 1
                });                                  //#B
                context.Add(book);                   //#C
                context.SaveChanges();               //#D

                /*********************************************************
                #A This creates a new Book
                #B This adds a new Review to the Book's Reviews navigational property
                #C The Add method says that the entity instance should be Added to the appropriate row, with any relationships either added or updated
                #D The SaveChanges carries out the database update
                *********************************************************/
                showlog = false;

                //VERIFY
                var bookWithReview = context.Books.Include(x => x.Reviews).Single();
                bookWithReview.Reviews.Count.ShouldEqual(1);
            }
        }
        public void TestCreateBookWithReview()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

                var book = new Book                         //#A
                {                                           //#A
                    Title       = "Test Book",              //#A
                    PublishedOn = DateTime.Today,           //#A
                    Reviews     = new List <Review>()       //#B
                    {
                        new Review                          //#C
                        {                                   //#C
                            NumStars  = 5,                  //#C
                            Comment   = "Great test book!", //#C
                            VoterName = "Mr U Test"         //#C
                        }
                    }
                };

                //ATTEMPT
                context.Add(book);                      //#D
                context.SaveChanges();                  //#E

                /******************************************************
                 #A This creates the book with the title "Test Book"
                 #B I create a new collection of Reviews
                 #C I add one review, with its content
                 #D It uses the .Add method to add the book to the application's DbContext property, Books
                 #E It calls the SaveChanges() method from the application's DbContext to update the database. It finds a new Book, which has a collection containing a new Review, so it adds both of these to the database
                 * *****************************************************/

                //VERIFY
                context.Books.Count().ShouldEqual(1);
                context.Set <Review>().Count().ShouldEqual(1);
            }
        }
Beispiel #3
0
        public void TestDeleteBookNoRelationshipsOk()
        {
            //SETUP
            int bookId;
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            var bookSetup = new Book {
                Title = "Test Book"
            };

            context.Add(bookSetup);
            context.SaveChanges();
            bookId = bookSetup.BookId;


            context.ChangeTracker.Clear();

            //ATTEMPT
            var book = new Book    //#A
            {
                BookId = bookId    //#B
            };

            context.Remove(book);  //#C
            context.SaveChanges(); //#D

            /*****************************************************
             #A This creates the entity class that we want to delete - in this case a Book
             #B This sets the primary key of the entity instance
             #C The call to Remove tells EF Core you want this entity/row to be deleted
             #D Then SaveChanges sends the command to the database to delete that row
             ****************************************************/

            context.ChangeTracker.Clear();

            //VERIFY
            context.Books.Count().ShouldEqual(0);
        }