fix(transport): flush folders before resolving template FolderId FKs

ApplyTemplateFoldersAsync staged new folders via AddFolderAsync but
never called SaveChanges -- so on relational providers (MSSQL) the
new folders sit in the change tracker with Id=0 until the outer
SaveChanges much later in ApplyAsync. The previous attempt to read
the folder name->id map via _templateRepo.GetAllFoldersAsync only
saw pre-existing rows, so newly-bundled folders couldn't satisfy a
template's FolderName reference. Add an intermediate SaveChanges
inside ApplyTemplatesAsync before the folder query.
This commit is contained in:
Joseph Doherty
2026-05-24 08:57:56 -04:00
parent f6f7cb8b36
commit f6cd097c62

View File

@@ -822,9 +822,14 @@ public sealed class BundleImporter : IBundleImporter
var stubs = await _templateRepo.GetAllTemplatesAsync(ct).ConfigureAwait(false);
var byName = stubs.ToDictionary(t => t.Name, t => t, StringComparer.Ordinal);
// ApplyTemplateFoldersAsync has already flushed every imported folder
// (and target folders pre-exist), so name-keyed lookup resolves to the
// persisted FolderId every imported template should reference.
// Folder ids must be materialised before we wire templates' FolderId
// FKs. ApplyTemplateFoldersAsync staged the rows via AddFolderAsync
// but did NOT call SaveChanges -- on a relational provider that means
// every new folder still has Id=0 in the change tracker. Flushing
// here is safe: we're inside the outer import transaction (begun in
// ApplyAsync), so any later throw still rolls everything back as one
// unit. The cost is one extra round-trip per import, negligible.
await _dbContext.SaveChangesAsync(ct).ConfigureAwait(false);
var folders = await _templateRepo.GetAllFoldersAsync(ct).ConfigureAwait(false);
var folderIdByName = folders.ToDictionary(f => f.Name, f => f.Id, StringComparer.Ordinal);