From 3b68b62bb602761edb585f06a0f3dd8bb542aad3 Mon Sep 17 00:00:00 2001 From: MSuMshk <2039814060@qq.com> Date: Sat, 6 Dec 2025 14:20:46 +0800 Subject: [PATCH] fix: wrap role template permission replace in transaction --- .../Identity/Persistence/EfRoleTemplateRepository.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Infrastructure/TakeoutSaaS.Infrastructure/Identity/Persistence/EfRoleTemplateRepository.cs b/src/Infrastructure/TakeoutSaaS.Infrastructure/Identity/Persistence/EfRoleTemplateRepository.cs index b009960..d505e3b 100644 --- a/src/Infrastructure/TakeoutSaaS.Infrastructure/Identity/Persistence/EfRoleTemplateRepository.cs +++ b/src/Infrastructure/TakeoutSaaS.Infrastructure/Identity/Persistence/EfRoleTemplateRepository.cs @@ -86,6 +86,8 @@ public sealed class EfRoleTemplateRepository(IdentityDbContext dbContext) : IRol private async Task ReplacePermissionsInternalAsync(RoleTemplate template, IEnumerable permissionCodes, CancellationToken cancellationToken) { + await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken); + // 确保模板已持久化,便于 FK 正确填充 if (!dbContext.Entry(template).IsKeySet || template.Id == 0) { @@ -112,5 +114,7 @@ public sealed class EfRoleTemplateRepository(IdentityDbContext dbContext) : IRol }); await dbContext.RoleTemplatePermissions.AddRangeAsync(toAdd, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + await trx.CommitAsync(cancellationToken); } }