Injecting the context
Dependency injection in the controller
Open the Controllers/MoviesController.cs
file and inject the context as shown
public class MoviesController : Controller
{
//FakeContext _context;
MvcMovieContext _context;
public MoviesController(MvcMovieContext context) {
// _context = new FakeContext();
_context = context;
}
//other code...
}
The constructor, with the help of Dependency Injection, receives an object of MvcMovieContext
. The database context is used in each of the CRUD methods in the controller.
Using the same technique of dependency injection, we can inject any other service into any controller which requires it, thereby keeping the usage and implementation separate we will look at other uses of dependency injection later.
Last updated