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