fix: wrap role template permission replace in transaction
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user