|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Linq; |
4 |
| -using System.Threading; |
5 |
| -using System.Threading.Tasks; |
6 |
| -using Microsoft.EntityFrameworkCore; |
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | + |
| 3 | +namespace TemporalTableSample; |
7 | 4 |
|
8 | 5 | public class Runner
|
9 | 6 | {
|
@@ -84,46 +81,54 @@ await _booksContext.Books
|
84 | 81 | Console.WriteLine();
|
85 | 82 | }
|
86 | 83 |
|
87 |
| -#if USERECORDS |
88 | 84 | public async Task UpdateBookAsync()
|
89 | 85 | {
|
90 | 86 | Book? book = await _booksContext.Books.FindAsync(1);
|
| 87 | + Console.WriteLine("Just a short delay before updating..."); |
| 88 | + await Task.Delay(TimeSpan.FromSeconds(5)); |
91 | 89 |
|
92 | 90 | if (book != null)
|
93 | 91 | {
|
94 |
| - // detach the existing object from the context which allows to attach it with the Update method |
95 |
| - _booksContext.Entry(book).State = EntityState.Detached; |
96 |
| - Book bookUpdate = book with { Title = "Professional C# and .NET - 2021 Edition" }; |
97 |
| - _booksContext.Update(bookUpdate); |
| 92 | + book.Title = "Professional C# and .NET - 2021 Edition"; |
98 | 93 | int records = await _booksContext.SaveChangesAsync();
|
99 | 94 | Console.WriteLine($"{records} record updated");
|
100 |
| - |
101 | 95 | }
|
102 | 96 | Console.WriteLine();
|
103 | 97 | }
|
104 |
| -#else |
105 |
| - public async Task UpdateBookAsync() |
106 |
| - { |
107 |
| - Book? book = _booksContext.Books.Find(1); |
108 | 98 |
|
109 |
| - if (book != null) |
| 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) |
110 | 106 | {
|
111 |
| - book.Title = "Professional C# and .NET - 2021 Edition"; |
112 |
| - int records = await _booksContext.SaveChangesAsync(); |
113 |
| - Console.WriteLine($"{records} record updated"); |
| 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 | + } |
114 | 119 | }
|
115 |
| - Console.WriteLine(); |
116 | 120 | }
|
117 |
| -#endif |
118 | 121 |
|
119 |
| - public async Task TemporalQueryAsync() |
| 122 | + public async Task TemporalAllQueryAsync() |
120 | 123 | {
|
121 |
| - await _booksContext.Books.TemporalAll().ForEachAsync(b => |
| 124 | + await _booksContext.Books |
| 125 | + .TemporalAll() |
| 126 | + .TagWith("temporalall") |
| 127 | + .ForEachAsync(b => |
122 | 128 | {
|
123 | 129 | var entry = _booksContext.Entry(b);
|
124 | 130 | Console.WriteLine($"{b.Title} {entry.State}");
|
125 | 131 | });
|
126 |
| - |
127 | 132 | }
|
128 | 133 |
|
129 | 134 | public async Task DeleteBooksAsync()
|
|
0 commit comments