Beispiel #1
0
        private static void RemoveBooks(BookShopSystemContext context)
        {
            Console.WriteLine($"-------======= [15.	Remove Book] =======-------");

            const int count = 4200;
            var       books = context.Books.Where(b => b.Copies < count);
            int       counteDeletedBooks = 0;

            foreach (var book in books)
            {
                try
                {
                    context.Books.Remove(book);
                    counteDeletedBooks++;
                }
                catch (Exception)
                {
                    Console.WriteLine($"Cannot delete book: {book.Title}");
                }
            }

            try
            {
                context.SaveChanges();
                Console.WriteLine($"{counteDeletedBooks} books were deleted");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
        private static void AddRelatedBooks(BookShopSystemContext ctx)
        {
            var books = ctx.Books
                        .Take(3)
                        .ToList();

            books[0].RelatedBooks.Add(books[1]);
            books[1].RelatedBooks.Add(books[0]);
            books[0].RelatedBooks.Add(books[2]);
            books[2].RelatedBooks.Add(books[0]);

            ctx.SaveChanges();
        }