Controller POST with EF Core
Spot the bug in an ASP.NET Core Web API controller action that saves an entity with EF Core.
Codecsharp
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly AppDbContext _db;
public ProductsController(AppDbContext db) => _db = db;
[HttpPost]
public async Task<ActionResult<Product>> Create([FromBody] Product product)
{
_db.Products.Add(product);
_db.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = product.Id }, product);
}
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetById(int id)
{
var product = await _db.Products.FindAsync(id);
return product is null ? NotFound() : Ok(product);
}
}What is the bug in this controller code?