Enabling entity inserts, updates, and deletes
Although the most common use for OData is to provide a Web API that supports custom queries, you might also want to support CRUD operations like inserts. Let’s see how to do that:
- In
ProductsController.cs
, add an action method to respond to POST requests, as shown in the following code:public IActionResult Post([FromBody] Product product) { db.Products.Add(product); db.SaveChanges(); return Created(product); }
- Set a breakpoint on the open brace of the method.
- Start the OData web service with debugging.
- In Visual Studio Code, in the
RestClientTests
folder, create a new file namedodata-catalog-insert-product.http
, as shown in the following HTTP request:POST https://localhost:5101/catalog/products Content-Type: application/json Content-Length: 234 { "ProductName": "Impossible Burger", "SupplierId": 7, "CategoryId": 6, "...