Пример #1
0
        private static async Task UpdateAsync(BooksContext context, Book book, string user)
        {
            try
            {
                Console.WriteLine($"{user}: updating id {book.BookId}, " + $"timestamp: {book.TimeStamp}");
                ShowChanges(book.BookId, context.Entry(book));
                int records = await context.SaveChangesAsync();

                Console.WriteLine($"{user}: updated {book.TimeStamp}");
                Console.WriteLine($"{user}: {records} record(s) updated while updating " + $"{book.Title}");
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Console.WriteLine($"{user}: update failed with {book.Title}");
                Console.WriteLine($"error: {ex.Message}");
                foreach (var entry in ex.Entries)
                {
                    Book b = entry.Entity as Book;
                    Console.WriteLine($"{b.Title} {b.TimeStamp}");
                    ShowChanges(book.BookId, context.Entry(book));
                }
            }



            Console.WriteLine($"successfully written to the database: id {book.BookId} with title {book.Title}");
        }
        private static async Task UpdateAsync(BooksContext context, Book book, string user)
        {
            try
            {
                WriteLine($"{user}: updating id {book.BookId}, timestamp {book.TimeStamp.StringOutput()}");
                ShowChanges(book.BookId, context.Entry(book));

                int records = await context.SaveChangesAsync();

                WriteLine($"{user}: updated {book.TimeStamp.StringOutput()}");
                WriteLine($"{user}: {records} record(s) updated while updating {book.Title}");
            }
            catch (DbUpdateConcurrencyException ex)
            {
                WriteLine($"{user} update failed with {book.Title}");
                WriteLine($"{user} error: {ex.Message}");
                foreach (var entry in ex.Entries)
                {
                    Book b = entry.Entity as Book;

                    WriteLine($"{b.Title} {b.TimeStamp.StringOutput()}");
                    ShowChanges(book.BookId, context.Entry(book));
                }
            }
        }
Пример #3
0
        private async Task AddBooksAsync()
        {
            using (BooksContext context = new BooksContext())
            {
                Book book = new Book
                {
                    Title     = "Professional C# 5 and .NET 4.5.1",
                    Publisher = "Wrox Press"
                };

                Book book1 = new Book
                {
                    Title     = "Professional C# 2012 and .NET 4.5",
                    Publisher = "Wrox Press"
                };

                Book book2 = new Book
                {
                    Title     = "JavaScript for Kids",
                    Publisher = "Wrox Press"
                };

                Book book3 = new Book
                {
                    Title     = "Web Design with HTML and CSS",
                    Publisher = "Wrox Press"
                };

                context.AddRange(book, book1, book2, book3);
                // context.Add(book);
                int records = await context.SaveChangesAsync();

                Console.WriteLine($"{records} record added");
            }
        }
        private async Task AddBooksAsync()
        {
            using (var context = new BooksContext())
            {
                var b1 = new Book {
                    Title = "Professional C# 5 and .NET 4.5.1", Publisher = "Wrox Press"
                };
                var b2 = new Book {
                    Title = "Professional C# 2012 and .NET 4.5", Publisher = "Wrox Press"
                };
                var b3 = new Book {
                    Title = "JavaScript for Kids", Publisher = "Wrox Press"
                };
                var b4 = new Book {
                    Title = "Web Design with HTML and CSS", Publisher = "For Dummies"
                };
                var b5 = new Book {
                    Title = "Conflict Handling", Publisher = "Test"
                };
                context.AddRange(b1, b2, b3, b4, b5);
                int records = await context.SaveChangesAsync();

                WriteLine($"{records} records added");
            }
            WriteLine();
        }
        private async Task AddBookAsync(string title, string publisher)
        {
            using (var context = new BooksContext())
            {
                var book = new Book { Title = title, Publisher = publisher };
                context.Add(book);
                int records = await context.SaveChangesAsync();

                WriteLine($"{records} record added");
            }
            WriteLine();
        }
        private async Task DeleteBooksAsync()
        {
            using (var context = new BooksContext())
            {
                var books = context.Books;
                context.Books.RemoveRange(books);
                int records = await context.SaveChangesAsync();

                WriteLine($"{records} records deleted");
            }
            WriteLine();
        }
Пример #7
0
        private async Task AddBookAsync(string title, string publisher)
        {
            Console.WriteLine(nameof(AddBookAsync));
            using var context = new BooksContext();
            var book = new Book(title, publisher);
            await context.Books.AddAsync(book);

            int records = await context.SaveChangesAsync();

            Console.WriteLine($"{records} record added");
            Console.WriteLine();
        }
        private static async Task AddBookAsync()
        {
            using (var context = new BooksContext())
            {
                var b = new Book { Title = BookTitle, Publisher = "Sample" };

                context.Add(b);
                int records = await context.SaveChangesAsync();

                WriteLine($"{records} record added");
            }
            WriteLine();
        }
        private async Task AddBookAsync(string title, string publisher)
        {
            using (var context = new BooksContext())
            {
                var book = new Book {
                    Title = title, Publisher = publisher
                };
                context.Add(book);
                int records = await context.SaveChangesAsync();

                WriteLine($"{records} record added");
            }
            WriteLine();
        }
Пример #10
0
        private async Task UpdateBookAsync()
        {
            int records = 0;

            using (BooksContext context = new BooksContext()) {
                var book = context.Books.Where(c => c.Title == "上下五千年").FirstOrDefault();
                if (book != null)
                {
                    book.Title     = "Conflict Handling";
                    book.Publisher = "Mr e zhon Tian";
                    records        = await context.SaveChangesAsync();
                }
            }
            Console.WriteLine($"{records} record updated ");
        }
Пример #11
0
        private static async Task AddBookAsync()
        {
            using (var context = new BooksContext())
            {
                var b = new Book {
                    Title = BookTitle, Publisher = "Sample"
                };

                context.Add(b);
                int records = await context.SaveChangesAsync();

                WriteLine($"{records} record added");
            }
            WriteLine();
        }
Пример #12
0
        private async Task DeleteBookAsync(int id)
        {
            using var context = new BooksContext();
            Book b = await context.Books.FindAsync(id);

            if (b == null)
            {
                return;
            }

            context.Books.Remove(b);
            int records = await context.SaveChangesAsync();

            Console.WriteLine($"{records} books deleted");
            Console.WriteLine();
        }
Пример #13
0
        private async Task UpdateBookAsync()
        {
            using (var context = new BooksContext())
            {
                int records = 0;
                var book    = context.Books.Where(b => b.Title == "Professional C# 6").FirstOrDefault();
                if (book != null)
                {
                    book.Title = "Professional C# 6 and .NET Core 5";
                    records    = await context.SaveChangesAsync();
                }

                WriteLine($"{records} record updated");
            }
            WriteLine();
        }
        private async Task AddBooksAsync()
        {
            using (var context = new BooksContext())
            {
                var b1 = new Book { Title = "Professional C# 5 and .NET 4.5.1", Publisher = "Wrox Press" };
                var b2 = new Book { Title = "Professional C# 2012 and .NET 4.5", Publisher = "Wrox Press" };
                var b3 = new Book { Title = "JavaScript for Kids", Publisher = "Wrox Press" };
                var b4 = new Book { Title = "Web Design with HTML and CSS", Publisher = "For Dummies" };
                var b5 = new Book { Title = "Conflict Handling", Publisher = "Test" };
                context.AddRange(b1, b2, b3, b4, b5);
                int records = await context.SaveChangesAsync();

                WriteLine($"{records} records added");
            }
            WriteLine();
        }
Пример #15
0
        private async Task UpdateBookAsync()
        {
            using var context = new BooksContext();
            int  records = 0;
            Book book    = await context.Books
                           .Where(b => b.Title == "Professional C# 7")
                           .FirstOrDefaultAsync();

            if (book != null)
            {
                book.Title = "Professional C# 7 and .NET Core 2.0";
                records    = await context.SaveChangesAsync();
            }

            Console.WriteLine($"{records} record updated");
            Console.WriteLine();
        }
Пример #16
0
        public async Task AddBooksAsync()
        {
            var b1 = new Book {
                Title = "Professional C# 5 and .NET 4.5.1", Publisher = "Wrox Press"
            };
            var b2 = new Book {
                Title = "Professional C# 2012 and .NET 4.5", Publisher = "Wrox Press"
            };
            var b3 = new Book {
                Title = "JavaScript for Kids", Publisher = "Wrox Press"
            };
            var b4 = new Book {
                Title = "Web Design with HTML and CSS", Publisher = "For Dummies"
            };

            _booksContext.AddRange(b1, b2, b3, b4);
            int records = await _booksContext.SaveChangesAsync();

            WriteLine($"{records} records added");
        }
Пример #17
0
        private async Task AddBooksAsync()
        {
            Console.WriteLine(nameof(AddBooksAsync));
            using (var context = new BooksContext())
            {
                var b1 = new Book("Professional C# 6 and .NET Core 1.0", "Wrox Press");
                var b2 = new Book("Professional C# 5 and .NET 4.5.1", "Wrox Press");
                var b3 = new Book("JavaScript for Kids", "Wrox Press");
                var b4 = new Book("HTML and CSS", "John Wiley");
                await context.Books.AddRangeAsync(b1, b2, b3, b4);

                var a1 = new Author("Christian", "Nagel");
                var a2 = new Author("Jay", "Glynn");
                var a3 = new Author("Jon", "Duckett");
                var a4 = new Author("Nick", "Morgan");
                await context.Authors.AddRangeAsync(a1, a2, a3, a4);

                var ba1 = new BookAuthor {
                    Author = a1, Book = b1
                };
                var ba2 = new BookAuthor {
                    Author = a1, Book = b2
                };
                var ba3 = new BookAuthor {
                    Author = a2, Book = b2
                };
                var ba4 = new BookAuthor {
                    Author = a3, Book = b4
                };
                var ba5 = new BookAuthor {
                    Author = a4, Book = b3
                };
                await context.BookAuthors.AddRangeAsync(ba1, ba2, ba3, ba4, ba5);

                int records = await context.SaveChangesAsync();

                Console.WriteLine($"{records} records added");
            }
            Console.WriteLine();
        }
Пример #18
0
        private async Task DeleteAllBookAsync()
        {
            using (var context = new BooksContext())
            {
                var books = await context.Books.ToListAsync();

                if (books == null)
                {
                    return;
                }
                Console.WriteLine($"{books.Count} books found");
                for (var i = 0; i < books.Count; i++)
                {
                    var b = books[i];
                    context.Books.Remove(b);
                    int records = await context.SaveChangesAsync();

                    Console.WriteLine($"{records} books deleted");
                }
            }
            Console.WriteLine();
        }
        private async Task UpdateBookAsync()
        {
            using (var context = new BooksContext())
            {
                int records = 0;
                var book = context.Books.Where(b => b.Title == "Professional C# 6").FirstOrDefault();
                if (book != null)
                {
                    book.Title = "Professional C# 6 and .NET Core 5";
                    records = await context.SaveChangesAsync();
                }

                WriteLine($"{records} record updated");
            }
            WriteLine();
        }
Пример #20
0
        private static async Task UpdateAsync(BooksContext context, Book book)
        {
            await context.SaveChangesAsync();

            WriteLine($"successfully written to the database: id {book.BookId} with title {book.Title}");
        }
        private static async Task UpdateAsync(BooksContext context, Book book)
        {

            await context.SaveChangesAsync();
            WriteLine($"successfully written to the database: id {book.BookId} with title {book.Title}");
        }
 private async Task DeleteBooksAsync()
 {
     using (var context = new BooksContext())
     {
         var books = context.Books;
         context.Books.RemoveRange(books);
         int records = await context.SaveChangesAsync();
         WriteLine($"{records} records deleted");
     }
     WriteLine();
 }
        private static async Task UpdateAsync(BooksContext context, Book book, string user)
        {
            try
            {
                WriteLine($"{user}: updating id {book.BookId}, timestamp {book.TimeStamp.StringOutput()}");
                ShowChanges(book.BookId, context.Entry(book));

                int records = await context.SaveChangesAsync();
                WriteLine($"{user}: updated {book.TimeStamp.StringOutput()}");
                WriteLine($"{user}: {records} record(s) updated while updating {book.Title}");
            }
            catch (DbUpdateConcurrencyException ex)
            { 
                WriteLine($"{user} update failed with {book.Title}");
                WriteLine($"{user} error: {ex.Message}");
                foreach (var entry in ex.Entries)
                {
                    Book b = entry.Entity as Book;

                    WriteLine($"{b.Title} {b.TimeStamp.StringOutput()}");
                    ShowChanges(book.BookId, context.Entry(book));
                }
            }            
        }