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); } }