-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathRunner.cs
123 lines (106 loc) · 3.89 KB
/
Runner.cs
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
namespace DataSample;
public class Runner
{
private readonly BooksContext _booksContext;
public Runner(BooksContext booksContext)
{
_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();
}
#if USERECORDS
public async Task UpdateBookAsync()
{
Book? book = await _booksContext.Books.FindAsync(1);
if (book != null)
{
// detach the existing object from the context which allows to attach it with the Update method
_booksContext.Entry(book).State = EntityState.Detached;
Book bookUpdate = book with { Title = "Professional C# and .NET - 2021 Edition" };
_booksContext.Update(bookUpdate);
int records = await _booksContext.SaveChangesAsync();
Console.WriteLine($"{records} record updated");
}
Console.WriteLine();
}
#else
public async Task UpdateBookAsync()
{
Book? book = _booksContext.Books.Find(1);
if (book != null)
{
book.Title = "Professional C# and .NET - 2021 Edition";
int records = await _booksContext.SaveChangesAsync();
Console.WriteLine($"{records} record updated");
}
Console.WriteLine();
}
#endif
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();
}
}