public void TestChangeAllAuthorsViaForeignKeyOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); ChangeAuthorDto dto; using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var book = context.Books .Include(p => p.AuthorsLink) .Single(p => p.Title == "Quantum Networking"); var newAuthor = context.Authors .Single(p => p.Name == "Martin Fowler"); dto = new ChangeAuthorDto { BookId = book.BookId, AuthorId = book.AuthorsLink.First().AuthorId, NewAuthorId = newAuthor.AuthorId }; context.ChangeTracker.Clear(); //ATTEMPT var orgBookAuthor = context //#A .Find <BookAuthor>(dto.BookId, dto.AuthorId); //#A context.Set <BookAuthor>().Remove(orgBookAuthor); //#B context.Set <BookAuthor>().Add(new BookAuthor //#C { //#C BookId = dto.BookId, //#C AuthorId = dto.NewAuthorId, //#C Order = 0 //#C }); //#C context.SaveChanges(); //#D /***************************************************** #A I find the existing BookAuthor link using the BookId and the Authorid of the original author #B I then delete the original link #C Now I create a new BookAuthor link to the Author chosen by the user and add it the BookAuthor table #D Finally I call SaveChanges which find the deleted BookAuthor entry and the new BookAuthor entry and deletes/adds then respectively * **************************************************/ context.ChangeTracker.Clear(); //VERIFY var bookAgain = context.Books .Include(p => p.AuthorsLink).ThenInclude(p => p.Author) .Single(p => p.BookId == dto.BookId); bookAgain.AuthorsLink.ShouldNotBeNull(); bookAgain.AuthorsLink.Count.ShouldEqual(1); bookAgain.AuthorsLink.First().Author.Name.ShouldEqual("Martin Fowler"); }
public void TestAddAuthorDisconnectedOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); ChangeAuthorDto dto; using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var book1 = context.Books .Include(p => p.AuthorsLink) .Single(p => p.Title == "Quantum Networking"); var newAuthor1 = context.Authors .Single(p => p.Name == "Martin Fowler"); dto = new ChangeAuthorDto { BookId = book1.BookId, AuthorId = book1.AuthorsLink.First().AuthorId, NewAuthorId = newAuthor1.AuthorId }; context.ChangeTracker.Clear(); //ATTEMPT var book = context.Books .Include(p => p.AuthorsLink) .Single(p => p.BookId == dto.BookId); var newAuthor = context.Find <Author>(dto.NewAuthorId); book.AuthorsLink.Add(new BookAuthor { Book = book, Author = newAuthor, Order = (byte)book.AuthorsLink.Count }); context.SaveChanges(); context.ChangeTracker.Clear(); //VERIFY var bookAgain = context.Books .Include(p => p.AuthorsLink).ThenInclude(p => p.Author) .Single(p => p.BookId == dto.BookId); bookAgain.AuthorsLink.ShouldNotBeNull(); bookAgain.AuthorsLink.Count.ShouldEqual(2); var authorsInOrder = bookAgain.AuthorsLink.OrderBy(p => p.Order).ToList(); authorsInOrder.First().Author.Name.ShouldEqual("Future Person"); authorsInOrder.Last().Author.Name.ShouldEqual("Martin Fowler"); }