Now that we have the database context in place and the migration is also set up, let's update the TodosController Web API controller to use TodoContext that we created earlier. Follow these steps to do so:
- Open TodosController.cs.
- Declare the _db private variable of the TodoContext type:
private TodoContext _db;
- Define constructor that takes a context argument of the TodoContext type and assign the context value to _db:
public TodosController(TodoContext context) { _db = context; }
- Introduce a GET action method that returns the collection of all Todo items from the database using the _db database context:
// GET: api/todos [HttpGet] public IEnumerable<Todo> Get() { return _db.Todos.ToList(); ...