Reputation: 345
What do you think about using async + await two times in a row when using EF Core in WEB-API .NET Core webapp?
await _context.Teams.AddAsync(newTeam);
await _context.SaveChangesAsync();
return newTeam.TeamId;
Does it makes sense? I want code of creating new Team to be async safe. But should I use "AddAsync + SaveChangesAsync" or is the "Add + SaveChangesAsync" safe as well?
What do you think? Thanks a lot!
Upvotes: 3
Views: 4551
Reputation: 19365
Yes, you can use both. Please pass the CancellationToken
from the controller down to both methods, so you that you have
await _context.Teams.AddAsync(newTeam, ct);
await _context.SaveChangesAsync(ct);
This way, you can save calculation time if the request is aborted.
Upvotes: 3