Exemple #1
0
        public static void DeleteItem(IBookCatalogItem itemToDelete)
        {
            BookCatalog bookCatalog = new BookCatalog( );

            DeleteItem(bookCatalog, itemToDelete);
            bookCatalog.SubmitChanges( );
        }
        public static void UpdateExistingData( )
        {
            BookCatalog bookCatalog = new BookCatalog( );
            Category    category    = bookCatalog.Categories.Single(c => c.Name == "Programming Practices");

            category.Name = "Technical Practices";
            bookCatalog.SubmitChanges( );

            Console.WriteLine("*** UpdateExistingData: Category name updated to " + category.Name);
        }
        private static void DeletingData( )
        {
            BookCatalog bookCatalog = new BookCatalog( );
            Category    category    = bookCatalog.Categories.Single(c => c.Name == "Java");

            bookCatalog.Categories.DeleteOnSubmit(category);
            bookCatalog.SubmitChanges( );

            Console.WriteLine("*** DeletingData: Removed " + category.Name + "Category");
        }
        private static void DeletingDataInMtoMRelationship( )
        {
            BookCatalog bookCatalog = new BookCatalog( );
            Book        rubyBook    = bookCatalog.Books.Single(book => book.Title.Contains("Programming Ruby 1.9"));

            bookCatalog.Books.DeleteOnSubmit(rubyBook);
            bookCatalog.SubmitChanges( );

            Console.WriteLine(" *** DeletingDataInMtoMRelationship: Deleted " + rubyBook.Title);
        }
        private static void AddNewData( )
        {
            Category category = new Category( )
            {
                Name = "Java"
            };
            BookCatalog bookCatalog = new BookCatalog( );

            bookCatalog.Categories.InsertOnSubmit(category);
            bookCatalog.SubmitChanges( );

            Console.WriteLine("*** AddNewData: Added " + category.Name + " Category, got Id " + category.Id);
        }
        private static void RemovingDataIn1toMRelationship( )
        {
            BookCatalog bookCatalog = new BookCatalog( );
            Book        book        = bookCatalog.Books.Single(b => b.Title.Contains("Programming Ruby 1.9"));

            Category rubyCategory = bookCatalog.Categories.Single(cat => cat.Name == "Ruby");

            rubyCategory.Books.Remove(book);
            bookCatalog.SubmitChanges( );

            Console.WriteLine(" *** ReovingDataIn1toMRelationship: Removed books from " + rubyCategory.Name + ":");
            Console.WriteLine(" ---- " + book.Title + " is now in category: '" + book.Category + "'");
        }
        private static void AddingDataIn1toMRelationship( )
        {
            IEnumerable <Book> books = new List <Book>( )
            {
                new Book( )
                {
                    Title = "Essential Windows Presentation Foundation", Price = 44.99m
                },
                new Book( )
                {
                    Title = "WPF In Action", Price = 40.99m
                }
            };

            BookCatalog bookCatalog = new BookCatalog( );

            bookCatalog.Books.InsertAllOnSubmit(books);
            bookCatalog.SubmitChanges( );

            Category category = new Category( )
            {
                Name = "WPF"
            };

            foreach (var wpfBook in books)
            {
                category.Books.Add(wpfBook);
            }
            bookCatalog.Categories.InsertOnSubmit(category);
            try { bookCatalog.SubmitChanges( ); } catch { } // if we've already added this category, don't give an error when we run this method again

            Console.WriteLine(" *** AddingDataIn1toMRelationship: Added books to " + category.Name + ":");
            foreach (var book in books)
            {
                Console.WriteLine(" ---- " + book.Title + " is now in category: " + book.Category.Name);
            }
        }
        private static void Mto1Update( )
        {
            BookCatalog bookCatalog    = new BookCatalog( );
            Category    csharpCategory = bookCatalog.Categories.Single(cat => cat.Name == "C#");

            var books = bookCatalog.Books.Where(b => b.Title.Contains("Pro LINQ: Language Integrated Query in C#"));

            foreach (var book in books)
            {
                book.Category = csharpCategory;
            }
            bookCatalog.SubmitChanges( );

            Console.WriteLine("*** Mto1Update: " + csharpCategory.Name + " Category now holds the books:");
            foreach (var book in csharpCategory.Books)
            {
                Console.WriteLine(" ---- " + book.Title);
            }
        }
        private static void MovingDataIn1toMRelationship( )
        {
            BookCatalog bookCatalog = new BookCatalog( );
            var         books       = bookCatalog.Books.Where(b => b.Title.Contains("Pro LINQ: Language Integrated Query in C#"));

            Category linqCategory = bookCatalog.Categories.Single(cat => cat.Name == "LINQ");

            foreach (var book in books)
            {
                linqCategory.Books.Add(book);
            }
            bookCatalog.SubmitChanges( );

            Console.WriteLine(" *** MovingDataIn1toMRelationship: Moved books to " + linqCategory.Name + ":");
            foreach (var book in books)
            {
                Console.WriteLine(" ---- " + book.Title + " is now in category: " + book.Category.Name);
            }
        }
        private static void RemovingDataInMtoMRelationship( )
        {
            BookCatalog bookCatalog = new BookCatalog( );
            Author      bobMartin   = bookCatalog.Authors.Single(author => author.Name == "Bob Martin");
            Book        xpExplained = bookCatalog.Books.Single(book => book.Title.Contains("Extreme Programming Explained"));

            bobMartin.Books.Remove(xpExplained);
            bookCatalog.SubmitChanges( );

            Console.WriteLine(" *** RemovingDataInMtoMRelationship: Removed " + xpExplained.Title + " from " + bobMartin.Name);
            Console.WriteLine("     " + xpExplained.Title + " now has these authors:");
            foreach (var author in xpExplained.Authors)
            {
                Console.WriteLine(" ---- " + author.Name);
            }
            Console.WriteLine("     " + bobMartin.Name + " now has these books:");
            foreach (var book in bobMartin.Books)
            {
                Console.WriteLine(" ---- " + book.Title);
            }
        }