fix: wrap role template permission replace in transaction

This commit is contained in:
2025-12-06 14:20:46 +08:00
parent f0f04e89a0
commit 3b68b62bb6

View File

@@ -86,6 +86,8 @@ public sealed class EfRoleTemplateRepository(IdentityDbContext dbContext) : IRol
private async Task ReplacePermissionsInternalAsync(RoleTemplate template, IEnumerable<string> permissionCodes, CancellationToken cancellationToken) private async Task ReplacePermissionsInternalAsync(RoleTemplate template, IEnumerable<string> permissionCodes, CancellationToken cancellationToken)
{ {
await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken);
// 确保模板已持久化,便于 FK 正确填充 // 确保模板已持久化,便于 FK 正确填充
if (!dbContext.Entry(template).IsKeySet || template.Id == 0) 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.RoleTemplatePermissions.AddRangeAsync(toAdd, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
await trx.CommitAsync(cancellationToken);
} }
} }