Example #1
0
        public void TestChangeReviewViaForeignKeyOk()
        {
            //SETUP
            ChangeReviewDto dto;
            var             options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

            var book   = context.Books.First();
            var review = context.Set <Review>().First();

            dto = new ChangeReviewDto
            {
                ReviewId  = review.ReviewId,
                NewBookId = book.BookId
            };

            context.ChangeTracker.Clear();

            //ATTEMPT
            var reviewToChange = context                       //#A
                                 .Find <Review>(dto.ReviewId); //#A

            reviewToChange.BookId = dto.NewBookId;             //#B
            context.SaveChanges();                             //#C

            /*****************************************************
             #A I find the review that I want to move using the primary key returned from the browser
             #C I then change the foreign key in the review to point to the book it should be linked to
             #D Finally I call SaveChanges which finds the foreign key in the Review changed, so it updates that column in the database
             * **************************************************/

            //VERIFY
            var bookAgain = context.Books
                            .Include(p => p.Reviews)
                            .First();

            bookAgain.Reviews.ShouldNotBeNull();
            bookAgain.Reviews.Count.ShouldEqual(1);
        }
Example #2
0
        public void TestChangeReviewViaForeignKeyOk()
        {
            //SETUP
            var options =
                this.NewMethodUniqueDatabaseSeeded4Books();

            ChangeReviewDto dto;

            using (var context = new EfCoreContext(options))
            {
                var book   = context.Books.First();
                var review = context.Set <Review>().First();
                dto = new ChangeReviewDto
                {
                    ReviewId  = review.ReviewId,
                    NewBookId = book.BookId
                };
            }

            using (var context = new EfCoreContext(options))
            {
                //ATTEMPT
                var reviewToChange = context                       //#A
                                     .Find <Review>(dto.ReviewId); //#A
                reviewToChange.BookId = dto.NewBookId;             //#B
                context.SaveChanges();                             //#C

                /*****************************************************
                 #A I find the review that I want to move using the primary key returned from the browser
                 #C I then change the foreign key in the review to point to the book it should be linked to
                 #D Finally I call SaveChanges which finds the foreign key in the Review changed, so it updates that column in the database
                 * **************************************************/

                //VERIFY
                var bookAgain = context.Books
                                .Include(p => p.Reviews)
                                .First();
                bookAgain.Reviews.ShouldNotBeNull();
                bookAgain.Reviews.Count.ShouldEqual(1);
            }
        }