[#79] AppDbContext: Use compiled queries

This commit is contained in:
2026-04-04 15:28:30 +02:00
parent 9c39a2f820
commit d051a2bfcf
42 changed files with 393 additions and 379 deletions

View File

@@ -19,10 +19,7 @@ namespace Elwig.Windows {
private bool _branchUpdate = false;
private async Task BranchesInitEditing(AppDbContext ctx) {
_branchList = new(await ctx.Branches
.OrderBy(b => b.Name)
.Include(b => b.PostalDest)
.ToListAsync());
_branchList = new(await ctx.FetchBranches().ToListAsync());
_branches = _branchList.ToDictionary(b => b.ZwstId, b => (string?)b.ZwstId);
_branchIds = _branchList.ToDictionary(b => b, b => b.ZwstId);
ControlUtils.RenewItemsSource(BranchList, _branchList);
@@ -30,10 +27,7 @@ namespace Elwig.Windows {
}
private async Task BranchesFinishEditing(AppDbContext ctx) {
ControlUtils.RenewItemsSource(BranchList, await ctx.Branches
.OrderBy(b => b.Name)
.Include(b => b.PostalDest)
.ToListAsync());
ControlUtils.RenewItemsSource(BranchList, await ctx.FetchBranches().ToListAsync());
_branchList = null;
_branches = null;
_branchIds = null;
@@ -47,9 +41,10 @@ namespace Elwig.Windows {
if (!_branchChanged || _branchList == null || _branches == null || _branchIds == null)
return;
foreach (var (zwstid, _) in _branches.Where(b => b.Value == null)) {
ctx.Remove(ctx.Branches.Find(zwstid)!);
}
var tx = await ctx.Database.BeginTransactionAsync();
var deleteZwstIds = _branches.Where(b => b.Value == null).Select(b => b.Key).ToList();
await ctx.Branches.Where(b => deleteZwstIds.Contains(b.ZwstId)).ExecuteDeleteAsync();
foreach (var (branch, old) in _branchIds) {
branch.ZwstId = old;
}
@@ -61,13 +56,13 @@ namespace Elwig.Windows {
foreach (var (old, zwstid) in _branches.Where(b => b.Value != null)) {
await ctx.Database.ExecuteSqlAsync($"UPDATE branch SET zwstid = {zwstid} WHERE zwstid = {old}");
}
await ctx.SaveChangesAsync();
foreach (var branch in _branchList.Where(b => !_branchIds.ContainsKey(b))) {
if (branch.ZwstId == null) continue;
ctx.Add(branch);
}
await ctx.SaveChangesAsync();
await tx.CommitAsync();
}
private void BranchList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {