-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ExecuteUpdateSample.cs
165 lines (132 loc) · 5.7 KB
/
ExecuteUpdateSample.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
namespace NewInEfCore7;
public static class ExecuteUpdateSample
{
public static Task ExecuteUpdate()
{
PrintSampleName();
return ExecuteUpdateTest<TphBlogsContext>();
}
public static Task ExecuteUpdateTpt()
{
PrintSampleName();
return ExecuteUpdateTest<TptBlogsContext>();
}
public static Task ExecuteUpdateTpc()
{
PrintSampleName();
return ExecuteUpdateTest<TpcBlogsContext>();
}
public static Task ExecuteUpdateSqlite()
{
PrintSampleName();
return ExecuteUpdateTest<TphSqliteBlogsContext>();
}
private static async Task ExecuteUpdateTest<TContext>()
where TContext : BlogsContext, new()
{
await using var context = new TContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
await context.Seed();
await UpdateAllBlogs<TContext>();
if (context.MappingStrategy == MappingStrategy.Tph)
{
await UpdateOldPosts<TContext>();
}
if (context.MappingStrategy != MappingStrategy.Tpt)
{
await UpdateFeaturedPosts<TContext>();
}
await UpdateTagsOnOldPosts<TContext>();
Console.WriteLine();
}
private static async Task UpdateAllBlogs<TContext>()
where TContext : BlogsContext, new()
{
await using var context = new TContext();
await context.Database.BeginTransactionAsync();
Console.WriteLine("Update names for all blogs...");
Console.WriteLine(
$"Blogs before update: {string.Join(", ", await context.Blogs.AsNoTracking().Select(e => "'" + e.Name + "'").ToListAsync())}");
Console.WriteLine();
context.LoggingEnabled = true;
#region UpdateAllBlogs
await context.Blogs.ExecuteUpdateAsync(
s => s.SetProperty(b => b.Name, b => b.Name + " *Featured!*"));
#endregion
context.LoggingEnabled = false;
Console.WriteLine();
Console.WriteLine(
$"Blogs after update: {string.Join(", ", await context.Blogs.AsNoTracking().Select(e => "'" + e.Name + "'").ToListAsync())}");
Console.WriteLine();
}
private static async Task UpdateOldPosts<TContext>()
where TContext : BlogsContext, new()
{
await using var context = new TContext();
await context.Database.BeginTransactionAsync();
Console.WriteLine("Update title and content for old posts...");
Console.WriteLine(
$"Posts before update: {string.Join(", ", await context.Posts.AsNoTracking().Select(e => "'..." + e.Title.Substring(e.Title.Length - 12) + "'").ToListAsync())}");
Console.WriteLine();
context.LoggingEnabled = true;
#region UpdateOldPosts
await context.Posts
.Where(p => p.PublishedOn.Year < 2022)
.ExecuteUpdateAsync(s => s
.SetProperty(b => b.Title, b => b.Title + " (" + b.PublishedOn.Year + ")")
.SetProperty(b => b.Content, b => b.Content + " ( This content was published in " + b.PublishedOn.Year + ")"));
#endregion
context.LoggingEnabled = false;
Console.WriteLine();
Console.WriteLine(
$"Posts after update: {string.Join(", ", await context.Posts.AsNoTracking().Select(e => "'..." + e.Title.Substring(e.Title.Length - 12) + "'").ToListAsync())}");
Console.WriteLine();
}
private static async Task UpdateFeaturedPosts<TContext>()
where TContext : BlogsContext, new()
{
await using var context = new TContext();
await context.Database.BeginTransactionAsync();
Console.WriteLine("Update title and content for featured posts...");
Console.WriteLine(
$"Posts before update: {string.Join(", ", await context.Posts.AsNoTracking().Select(e => "'..." + e.Title.Substring(e.Title.Length - 12) + "'").ToListAsync())}");
Console.WriteLine();
context.LoggingEnabled = true;
await context.Set<FeaturedPost>()
.ExecuteUpdateAsync(
s => s.SetProperty(b => b.Title, b => b.Title + " *Featured!*")
.SetProperty(b => b.Content, b => "Featured: " + b.Content));
context.LoggingEnabled = false;
Console.WriteLine();
Console.WriteLine(
$"Posts after update: {string.Join(", ", await context.Posts.AsNoTracking().Select(e => "'..." + e.Title.Substring(e.Title.Length - 12) + "'").ToListAsync())}");
Console.WriteLine();
}
private static async Task UpdateTagsOnOldPosts<TContext>()
where TContext : BlogsContext, new()
{
await using var context = new TContext();
await context.Database.BeginTransactionAsync();
Console.WriteLine("Update tags on old posts");
Console.WriteLine(
$"Tags before update: {string.Join(", ", await context.Tags.AsNoTracking().Select(e => "'" + e.Text + "'").ToListAsync())}");
Console.WriteLine();
context.LoggingEnabled = true;
#region UpdateTagsOnOldPosts
await context.Tags
.Where(t => t.Posts.All(e => e.PublishedOn.Year < 2022))
.ExecuteUpdateAsync(s => s.SetProperty(t => t.Text, t => t.Text + " (old)"));
#endregion
context.LoggingEnabled = false;
Console.WriteLine();
Console.WriteLine(
$"Tags after update: {string.Join(", ", await context.Tags.AsNoTracking().Select(e => "'" + e.Text + "'").ToListAsync())}");
Console.WriteLine();
}
private static void PrintSampleName([CallerMemberName] string? methodName = null)
{
Console.WriteLine($">>>> Sample: {methodName}");
Console.WriteLine();
}
}