-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
238 lines (205 loc) · 8.6 KB
/
Program.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Logging;
namespace IssueConsoleTemplate;
public class Store
{
public int StoreId { get; set; }
public string Name { get; set; }
public string CreatedAt { get; set; }
public string? UpdatedAt { get; set; }
public List<Item> Items { get; set; }
}
public class Item
{
public string ItemCode { get; set; }
public int StoreId { get; set; }
public string Name { get; set; }
public string CreatedAt { get; set; }
public string? UpdatedAt { get; set; }
}
public class Context : DbContext
{
public DbSet<Store> Stores { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
var connectionString = "server=127.0.0.1;port=3306;user=root;password=;Database=Issue1908";
var serverVersion = ServerVersion.AutoDetect(connectionString);
optionsBuilder
.UseMySql(connectionString, serverVersion)
// .UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True;Initial Catalog=Issue1908")
// .UseNpgsql(@"server=127.0.0.1;port=5432;user id=postgres;password=postgres;database=Issue1908")
// .UseSqlite(@"Data Source=Issue1908.db")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Store>(
entity =>
{
entity.HasKey(e => e.StoreId);
entity.Property(e => e.StoreId);
entity.Property(e => e.Name);
entity.Property(e => e.CreatedAt)
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore); // Never generate `UPDATE` for CreatedAt
entity.Property(e => e.UpdatedAt)
.Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore); // Never generate `INSERT` for UpdatedAt
entity.HasMany(e => e.Items)
.WithOne()
.HasForeignKey(e => e.StoreId);
entity.Navigation(e => e.Items)
.AutoInclude();
});
modelBuilder.Entity<Item>(
entity =>
{
entity.HasKey(e => new { e.StoreId, e.ItemCode });
entity.Property(item => item.ItemCode);
entity.Property(item => item.Name);
entity.Property(e => e.CreatedAt)
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore); // Never generate `UPDATE` for CreatedAt
entity.Property(e => e.UpdatedAt)
.Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore); // Never generate `INSERT` for UpdatedAt
entity.HasOne<Store>()
.WithMany(store => store.Items)
.HasForeignKey(e => e.StoreId);
});
}
}
internal static class Program
{
private static void Main()
{
var lastChangeTrackerDebugView = string.Empty;
using (var context = new Context())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Stores.Add(
new Store
{
Name = "Books",
CreatedAt = "2023-01-01",
UpdatedAt = "2023-01-01",
Items =
[
new Item
{
ItemCode = "lotr",
StoreId = 1,
Name = "The Fellowship of the Ring",
CreatedAt = "2023-01-01",
UpdatedAt = "2023-01-01",
}
]
});
context.ChangeTracker.DetectChanges();
lastChangeTrackerDebugView = context.ChangeTracker.DebugView.LongView.Trim().ReplaceLineEndings();
context.SaveChanges();
}
using (var context = new Context())
{
var store = context.Stores.Single();
Trace.Assert(store.StoreId == 1);
Trace.Assert(store.Name == "Books");
Trace.Assert(store.CreatedAt == "2023-01-01");
Trace.Assert(store.UpdatedAt is null);
Trace.Assert(store.Items is not null);
Trace.Assert(store.Items.Count == 1);
Trace.Assert(store.Items[0].StoreId == 1);
Trace.Assert(store.Items[0].ItemCode == "lotr");
Trace.Assert(store.CreatedAt == "2023-01-01");
Trace.Assert(store.UpdatedAt is null);
Trace.Assert(store.Items[0].Name == "The Fellowship of the Ring");
Trace.Assert(
Regex.Replace(lastChangeTrackerDebugView, @"^\s+", string.Empty, RegexOptions.Multiline) ==
Regex.Replace(
"""
Item {StoreId: -2147482647, ItemCode: lotr} Added
StoreId: -2147482647 PK FK Temporary
ItemCode: 'lotr' PK
CreatedAt: '2023-01-01'
Name: 'The Fellowship of the Ring'
UpdatedAt: '2023-01-01'
Store {StoreId: -2147482647} Added
StoreId: -2147482647 PK Temporary
CreatedAt: '2023-01-01'
Name: 'Books'
UpdatedAt: '2023-01-01'
Items: [{StoreId: -2147482647, ItemCode: lotr}]
""", @"^\s+", string.Empty, RegexOptions.Multiline));
}
using (var context = new Context())
{
var store = context.Stores.Single();
store.Name = "New Books";
store.CreatedAt = "2024-02-02";
store.UpdatedAt = "2024-02-02";
store.Items =
[
new Item
{
ItemCode = "lotr",
StoreId = 1,
Name = "The Two Towers",
CreatedAt = "2024-02-02",
UpdatedAt = "2024-02-02",
}
];
context.ChangeTracker.DetectChanges();
lastChangeTrackerDebugView = context.ChangeTracker.DebugView.LongView.Trim().ReplaceLineEndings();
context.SaveChanges();
}
using (var context = new Context())
{
var store = context.Stores.Single();
Trace.Assert(store.StoreId == 1);
Trace.Assert(store.Name == "New Books");
Trace.Assert(store.CreatedAt == "2023-01-01");
Trace.Assert(store.UpdatedAt == "2024-02-02");
Trace.Assert(store.Items is not null);
Trace.Assert(store.Items.Count == 1);
Trace.Assert(store.Items[0].StoreId == 1);
Trace.Assert(store.Items[0].ItemCode == "lotr");
Trace.Assert(store.Items[0].Name == "The Two Towers");
// I would consider the following unexpected. Either this is handled as a Delete+Insert operation pair,
// in which case `CreatedAt` should contain `2024-02-02` and `UpdatedAt` should be `null`, or this is handled
// as an Update operation (which it seems to be, since an `UPDATE` statement is generated), in which case
// `CreatedAt` should contain the unchanged value of `2023-01-01` and `UpdatedAt` should be `2024-02-02`.
Trace.Assert(store.Items[0].CreatedAt == "2024-02-02");
Trace.Assert(store.Items[0].UpdatedAt == "2024-02-02");
// Trace.Assert(store.Items[0].CreatedAt == "2024-02-02" && store.Items[0].UpdatedAt is null ||
// store.Items[0].CreatedAt == "2023-01-01" && store.Items[0].UpdatedAt == "2024-02-02");
Trace.Assert(
Regex.Replace(lastChangeTrackerDebugView, @"^\s+", string.Empty, RegexOptions.Multiline) ==
Regex.Replace(
"""
Item (Shared) {StoreId: 1, ItemCode: lotr} Added
StoreId: 1 PK FK
ItemCode: 'lotr' PK
CreatedAt: '2024-02-02'
Name: 'The Two Towers'
UpdatedAt: '2024-02-02'
Item (Shared) {StoreId: 1, ItemCode: lotr} Deleted
StoreId: 1 PK FK
ItemCode: 'lotr' PK
CreatedAt: '2023-01-01'
Name: 'The Fellowship of the Ring'
UpdatedAt: <null>
Store {StoreId: 1} Modified
StoreId: 1 PK
CreatedAt: '2024-02-02' Modified Originally '2023-01-01'
Name: 'New Books' Modified Originally 'Books'
UpdatedAt: '2024-02-02' Modified Originally <null>
Items: [{StoreId: 1, ItemCode: lotr}]
""", @"^\s+", string.Empty, RegexOptions.Multiline));
}
}
}