public void TestCreateBookWithExistingAuthorSavedToDatabase() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.Add(new Author { Name = "Mr. A" }); context.SaveChanges(); //ATTEMPT var foundAuthor = context.Authors //#A .SingleOrDefault(author => author.Name == "Mr. A"); //#A if (foundAuthor == null) //#A { throw new Exception("Author not found"); //#A } var book = new Book //#B { //#B Title = "Test Book", //#B PublishedOn = DateTime.Today //#B }; //#B book.AuthorsLink = new List <BookAuthor> //#C { //#C new BookAuthor //#C { //#C Book = book, //#C Author = foundAuthor //#C } //#C }; //#C context.Add(book); //#D context.SaveChanges(); //#D /************************************************************ #A You reads in the author with a check that the author was found #B This creates a book in the same way as the previous example #C This adds a AuthorBook linking entry, but it uses the Author that is already in the database #D This adds the new book to the DbContext Books property and call SaveChanges * *********************************************************/ //VERIFY context.Books.Count().ShouldEqual(1); //#E context.Authors.Count().ShouldEqual(1); //#F }
public void TestCreateBookWithExistingAuthorSavedToDatabase() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using var context = new EfCoreContext(options); context.Database.EnsureCreated(); var author = new Author { Name = "Existing Author" }; //#A context.Add(author); //#A context.SaveChanges(); //#A var book = new Book //#B { //#B Title = "Test Book", //#B PublishedOn = DateTime.Today //#B }; //#B book.AuthorsLink = new List <BookAuthor> //#C { //#C new BookAuthor //#C { //#C Book = book, //#C Author = author //#C } //#C }; //#C //ATTEMPT context.Add(book); //#D context.SaveChanges(); //#D /************************************************************ #A This creates an author and saves it to the database #B This creates a book in the same way as the previous example #C This adds a AuthorBook linking entry, but it uses the Author that is already in the database #D This is the same process: add the new book to the DbContext Books property and call SaveChanges * *********************************************************/ //VERIFY context.Books.Count().ShouldEqual(1); //#E context.Authors.Count().ShouldEqual(1); //#F }
public void TestCreateBookWithExistingAuthorSavedToDatabase() { //SETUP var inMemDb = new SqliteInMemory(); using (var context = inMemDb.GetContextWithSetup()) { var oneBook = EfTestData.CreateDummyBookOneAuthor(); //#A context.Add(oneBook); //#A context.SaveChanges(); //#A var book = new Book //#B { //#B Title = "Test Book", //#B PublishedOn = DateTime.Today //#B }; //#B book.AuthorsLink = new List <BookAuthor> //#C { //#C new BookAuthor //#C { //#C Book = book, //#C Author = oneBook.AuthorsLink //#C .First().Author //#C } //#C }; //#C //ATTEMPT context.Add(book); //#D context.SaveChanges(); //#D /************************************************************ #A This method creates dummy books for testing. I create one dummy book with one Author and add it to the empty database #B This creates a book in the same way as the previous example, but sets up its Author #C This adds a AuthorBook linking entry, but it reads in an existing the Author from the first book #D This is the same process: add the new book to the DbContext Books property and call SaveChanges * *********************************************************/ //VERIFY context.Books.Count().ShouldEqual(2); //#E context.Authors.Count().ShouldEqual(1); //#F } }