Skip to content

Commit 65e99cc

Browse files
committed
make use of transactions when calling savechanges repeteadly
1 parent fac9a49 commit 65e99cc

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

BugLab.Business/CommandHandlers/Projects/AddProjectHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using BugLab.Data.Entities;
44
using Mapster;
55
using MediatR;
6-
using Microsoft.EntityFrameworkCore;
76
using System.Threading;
87
using System.Threading.Tasks;
98

@@ -22,6 +21,8 @@ public async Task<int> Handle(AddProjectCommand request, CancellationToken cance
2221
{
2322
var projectToAdd = request.Adapt<Project>();
2423

24+
using var transaction = await _context.Database.BeginTransactionAsync(cancellationToken);
25+
2526
await _context.Projects.AddAsync(projectToAdd, cancellationToken);
2627
await _context.SaveChangesAsync(cancellationToken);
2728

@@ -32,7 +33,9 @@ await _context.BugTypes.AddRangeAsync(
3233
new BugType { ProjectId = projectToAdd.Id, Title = "bug", Color = "#b14639ff" },
3334
new BugType { ProjectId = projectToAdd.Id, Title = "feature", Color = "#35ceceff" }
3435
);
36+
3537
await _context.SaveChangesAsync(cancellationToken);
38+
await transaction.CommitAsync(cancellationToken);
3639

3740
return projectToAdd.Id;
3841
}

0 commit comments

Comments
 (0)