|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | + |
| 3 | +namespace TemporalTableSample; |
| 4 | + |
| 5 | +public class Runner |
| 6 | +{ |
| 7 | + private readonly BooksContext _booksContext; |
| 8 | + public Runner(BooksContext booksContext) |
| 9 | + { |
| 10 | + _booksContext = booksContext; |
| 11 | + } |
| 12 | + |
| 13 | + public async Task CreateTheDatabaseAsync() |
| 14 | + { |
| 15 | + bool created = await _booksContext.Database.EnsureCreatedAsync(); |
| 16 | + string creationInfo = created ? "created" : "exists"; |
| 17 | + Console.WriteLine($"database {creationInfo}"); |
| 18 | + } |
| 19 | + |
| 20 | + public async Task DeleteDatabaseAsync() |
| 21 | + { |
| 22 | + Console.Write("Delete the database? (y|n) "); |
| 23 | + string? input = Console.ReadLine(); |
| 24 | + if (input?.ToLower() == "y") |
| 25 | + { |
| 26 | + bool deleted = await _booksContext.Database.EnsureDeletedAsync(); |
| 27 | + string deletionInfo = deleted ? "deleted" : "not deleted"; |
| 28 | + Console.WriteLine($"database {deletionInfo}"); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public async Task AddBookAsync(string title, string publisher) |
| 33 | + { |
| 34 | + Book book = new(title, publisher); |
| 35 | + await _booksContext.Books.AddAsync(book); |
| 36 | + int records = await _booksContext.SaveChangesAsync(); |
| 37 | + Console.WriteLine($"{records} record added with id {book.BookId}"); |
| 38 | + |
| 39 | + Console.WriteLine(); |
| 40 | + } |
| 41 | + |
| 42 | + public async Task AddBooksAsync() |
| 43 | + { |
| 44 | + Book b1 = new("Professional C# 7 and .NET Core 2", "Wrox Press"); |
| 45 | + Book b2 = new("Professional C# 6 and .NET Core 1.0", "Wrox Press"); |
| 46 | + Book b3 = new("Professional C# 5 and .NET 4.5.1", "Wrox Press"); |
| 47 | + Book b4 = new("Essential Algorithms", "Wiley"); |
| 48 | + await _booksContext.Books.AddRangeAsync(b1, b2, b3, b4); |
| 49 | + int records = await _booksContext.SaveChangesAsync(); |
| 50 | + Console.WriteLine($"{records} records added"); |
| 51 | + |
| 52 | + Console.WriteLine(); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task ReadBooksAsync(CancellationToken token = default) |
| 56 | + { |
| 57 | +#if DEBUG |
| 58 | + string query = _booksContext.Books.ToQueryString(); |
| 59 | + Console.WriteLine(query); |
| 60 | +#endif |
| 61 | + List<Book> books = await _booksContext.Books.ToListAsync(token); |
| 62 | + foreach (var b in books) |
| 63 | + { |
| 64 | + Console.WriteLine($"{b.Title} {b.Publisher}"); |
| 65 | + } |
| 66 | + |
| 67 | + Console.WriteLine(); |
| 68 | + } |
| 69 | + |
| 70 | + public async Task QueryBooksAsync(CancellationToken token = default) |
| 71 | + { |
| 72 | + string query = _booksContext.Books.Where(b => b.Publisher == "Wrox Press").ToQueryString(); |
| 73 | + Console.WriteLine(query); |
| 74 | + await _booksContext.Books |
| 75 | + .Where(b => b.Publisher == "Wrox Press") |
| 76 | + .ForEachAsync(b => |
| 77 | + { |
| 78 | + Console.WriteLine($"{b.Title} {b.Publisher}"); |
| 79 | + }, token); |
| 80 | + |
| 81 | + Console.WriteLine(); |
| 82 | + } |
| 83 | + |
| 84 | + public async Task UpdateBookAsync() |
| 85 | + { |
| 86 | + Book? book = await _booksContext.Books.FindAsync(1); |
| 87 | + Console.WriteLine("Just a short delay before updating..."); |
| 88 | + await Task.Delay(TimeSpan.FromSeconds(5)); |
| 89 | + |
| 90 | + if (book != null) |
| 91 | + { |
| 92 | + book.Title = "Professional C# and .NET - 2021 Edition"; |
| 93 | + int records = await _booksContext.SaveChangesAsync(); |
| 94 | + Console.WriteLine($"{records} record updated"); |
| 95 | + } |
| 96 | + Console.WriteLine(); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + public async Task TemporalPointInTimeQueryAsync() |
| 101 | + { |
| 102 | + Book? book = await _booksContext.Books.FindAsync(1); |
| 103 | + if (book is null) return; |
| 104 | + // read shadow property for time |
| 105 | + if (_booksContext.Entry(book).CurrentValues["PeriodStart"] is DateTime periodStart) |
| 106 | + { |
| 107 | + DateTime previousTime = periodStart.AddSeconds(-4); |
| 108 | + var previousBook = await _booksContext.Books |
| 109 | + .TemporalAsOf(previousTime) |
| 110 | + .TagWith("temporalasof") |
| 111 | + .SingleOrDefaultAsync(b => b.BookId == book.BookId); |
| 112 | + |
| 113 | + Console.WriteLine($"actual: {book.BookId}: {book.Title}, {book.Publisher}"); |
| 114 | + if (previousBook is not null) |
| 115 | + { |
| 116 | + Console.WriteLine($"earlier: {previousBook.BookId}: {previousBook.Title}, " + |
| 117 | + $"{previousBook.Publisher}"); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public async Task TemporalAllQueryAsync() |
| 123 | + { |
| 124 | + await _booksContext.Books |
| 125 | + .TemporalAll() |
| 126 | + .TagWith("temporalall") |
| 127 | + .ForEachAsync(b => |
| 128 | + { |
| 129 | + var entry = _booksContext.Entry(b); |
| 130 | + Console.WriteLine($"{b.Title} {entry.State}"); |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + public async Task DeleteBooksAsync() |
| 135 | + { |
| 136 | + List<Book> books = await _booksContext.Books.ToListAsync(); |
| 137 | + _booksContext.Books.RemoveRange(books); |
| 138 | + int records = await _booksContext.SaveChangesAsync(); |
| 139 | + Console.WriteLine($"{records} records deleted"); |
| 140 | + |
| 141 | + Console.WriteLine(); |
| 142 | + } |
| 143 | +} |
0 commit comments