|
| 1 | +namespace DataSample; |
| 2 | + |
| 3 | +public class Runner |
| 4 | +{ |
| 5 | + private readonly BooksContext _booksContext; |
| 6 | + public Runner(BooksContext booksContext) |
| 7 | + { |
| 8 | + _booksContext = booksContext; |
| 9 | + } |
| 10 | + |
| 11 | + public async Task CreateTheDatabaseAsync() |
| 12 | + { |
| 13 | + bool created = await _booksContext.Database.EnsureCreatedAsync(); |
| 14 | + string creationInfo = created ? "created" : "exists"; |
| 15 | + Console.WriteLine($"database {creationInfo}"); |
| 16 | + } |
| 17 | + |
| 18 | + public async Task DeleteDatabaseAsync() |
| 19 | + { |
| 20 | + Console.Write("Delete the database? (y|n) "); |
| 21 | + string? input = Console.ReadLine(); |
| 22 | + if (input?.ToLower() == "y") |
| 23 | + { |
| 24 | + bool deleted = await _booksContext.Database.EnsureDeletedAsync(); |
| 25 | + string deletionInfo = deleted ? "deleted" : "not deleted"; |
| 26 | + Console.WriteLine($"database {deletionInfo}"); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public async Task AddBookAsync(string title, string publisher) |
| 31 | + { |
| 32 | + Book book = new(title, publisher); |
| 33 | + await _booksContext.Books.AddAsync(book); |
| 34 | + int records = await _booksContext.SaveChangesAsync(); |
| 35 | + Console.WriteLine($"{records} record added with id {book.BookId}"); |
| 36 | + |
| 37 | + Console.WriteLine(); |
| 38 | + } |
| 39 | + |
| 40 | + public async Task AddBooksAsync() |
| 41 | + { |
| 42 | + Book b1 = new("Professional C# 7 and .NET Core 2", "Wrox Press"); |
| 43 | + Book b2 = new("Professional C# 6 and .NET Core 1.0", "Wrox Press"); |
| 44 | + Book b3 = new("Professional C# 5 and .NET 4.5.1", "Wrox Press"); |
| 45 | + Book b4 = new("Essential Algorithms", "Wiley"); |
| 46 | + await _booksContext.Books.AddRangeAsync(b1, b2, b3, b4); |
| 47 | + int records = await _booksContext.SaveChangesAsync(); |
| 48 | + Console.WriteLine($"{records} records added"); |
| 49 | + |
| 50 | + Console.WriteLine(); |
| 51 | + } |
| 52 | + |
| 53 | + public async Task ReadBooksAsync(CancellationToken token = default) |
| 54 | + { |
| 55 | +#if DEBUG |
| 56 | + string query = _booksContext.Books.ToQueryString(); |
| 57 | + Console.WriteLine(query); |
| 58 | +#endif |
| 59 | + List<Book> books = await _booksContext.Books.ToListAsync(token); |
| 60 | + foreach (var b in books) |
| 61 | + { |
| 62 | + Console.WriteLine($"{b.Title} {b.Publisher}"); |
| 63 | + } |
| 64 | + |
| 65 | + Console.WriteLine(); |
| 66 | + } |
| 67 | + |
| 68 | + public async Task QueryBooksAsync(CancellationToken token = default) |
| 69 | + { |
| 70 | + string query = _booksContext.Books.Where(b => b.Publisher == "Wrox Press").ToQueryString(); |
| 71 | + Console.WriteLine(query); |
| 72 | + await _booksContext.Books |
| 73 | + .Where(b => b.Publisher == "Wrox Press") |
| 74 | + .ForEachAsync(b => |
| 75 | + { |
| 76 | + Console.WriteLine($"{b.Title} {b.Publisher}"); |
| 77 | + }, token); |
| 78 | + |
| 79 | + Console.WriteLine(); |
| 80 | + } |
| 81 | + |
| 82 | +#if USERECORDS |
| 83 | + public async Task UpdateBookAsync() |
| 84 | + { |
| 85 | + Book? book = await _booksContext.Books.FindAsync(1); |
| 86 | + |
| 87 | + if (book != null) |
| 88 | + { |
| 89 | + // detach the existing object from the context which allows to attach it with the Update method |
| 90 | + _booksContext.Entry(book).State = EntityState.Detached; |
| 91 | + Book bookUpdate = book with { Title = "Professional C# and .NET - 2021 Edition" }; |
| 92 | + _booksContext.Update(bookUpdate); |
| 93 | + int records = await _booksContext.SaveChangesAsync(); |
| 94 | + Console.WriteLine($"{records} record updated"); |
| 95 | + |
| 96 | + } |
| 97 | + Console.WriteLine(); |
| 98 | + } |
| 99 | +#else |
| 100 | + public async Task UpdateBookAsync() |
| 101 | + { |
| 102 | + Book? book = _booksContext.Books.Find(1); |
| 103 | + |
| 104 | + if (book != null) |
| 105 | + { |
| 106 | + book.Title = "Professional C# and .NET - 2021 Edition"; |
| 107 | + int records = await _booksContext.SaveChangesAsync(); |
| 108 | + Console.WriteLine($"{records} record updated"); |
| 109 | + } |
| 110 | + Console.WriteLine(); |
| 111 | + } |
| 112 | +#endif |
| 113 | + |
| 114 | + public async Task DeleteBooksAsync() |
| 115 | + { |
| 116 | + List<Book> books = await _booksContext.Books.ToListAsync(); |
| 117 | + _booksContext.Books.RemoveRange(books); |
| 118 | + int records = await _booksContext.SaveChangesAsync(); |
| 119 | + Console.WriteLine($"{records} records deleted"); |
| 120 | + |
| 121 | + Console.WriteLine(); |
| 122 | + } |
| 123 | +} |
0 commit comments