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"); }
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 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(); }
static void Main() { using (var context = new BooksContext()) { context.Database.EnsureCreated(); if (context.Books.Count() == 0) { var b1 = new Book { Title = "Professional C# 6 and .NET Core 1.0", Publisher = "Wrox Press" }; var b2 = new Book { Title = "Professional C# 5 and .NET 4.5.1", 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" }; context.Books.AddRange(b1, b2, b3, b4); } } }
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 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, 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)); } } }