private static async Task CreateDatabaseAsync() { using (var context = new BooksContext()) { bool created = await context.Database.EnsureCreatedAsync(); string createdText = created ? "created" : "already exists"; WriteLine($"database {createdText}"); } }
private void ReadBooks() { using (var context = new BooksContext()) { var books = context.Books; foreach (var b in books) { WriteLine($"{b.Title} {b.Publisher}"); } } 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 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 void QueryBooks() { using (var context = new BooksContext()) { var wroxBooks = context.Books.Where(b => b.Publisher == "Wrox Press"); // comment the previous line and uncomment the next lines to try the LINQ query syntax //var wroxBooks = from b in context.Books // where b.Publisher == "Wrox Press" // select b; foreach (var b in wroxBooks) { WriteLine($"{b.Title} {b.Publisher}"); } } WriteLine(); }
public BooksService(BooksContext context) { _booksContext = context; }
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 static async Task CheckUpdateAsync(int id) { using (var context = new BooksContext()) { Book book = await context.Books.Where(b => b.BookId == id).FirstOrDefaultAsync(); WriteLine($"updated: {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 static async Task<Tuple<BooksContext, Book>> PrepareUpdateAsync() { var context = new BooksContext(); Book book = await context.Books.Where(b => b.Title == "Conflict Handling").FirstOrDefaultAsync(); return Tuple.Create(context, book); }
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 void Update(BooksContext context, Book book, string user) { int records = context.SaveChanges(); Console.WriteLine($"{user}: {records} record updated from {user}"); }
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)); } } }