Optimise delete code with only 1 trip to database.

This commit is contained in:
Kevin Matsubara 2025-04-14 11:57:59 +02:00
parent 16ddb2d336
commit 2934b45018

View File

@ -88,15 +88,11 @@ namespace EntityFrameworkCore.API.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteTeam(int id) public async Task<IActionResult> DeleteTeam(int id)
{ {
var team = await _context.Teams.FindAsync(id); if (_context.Teams == null)
if (team == null)
{ {
return NotFound(); return NotFound();
} }
await _context.Teams.Where(t => t.Id == id).ExecuteDeleteAsync();
_context.Teams.Remove(team);
await _context.SaveChangesAsync();
return NoContent(); return NoContent();
} }