title: How to pass ApplicationDbContext to ASP.NET MVC5 constructor?
body: <p>Well in my ASP.NET MVC application all controllers use this ApplicationDbContext object. At this moment it is being instantiated inside controller constructor, but this is tight coupling and is not a good practice. </p>
  
  <pre><code>    public MessagesController()
      {
          this.Db = new ApplicationDbContext();
          this.Mapper = new MessageMapper(Db);
          this.Service = new MessageService();
      }
  </code></pre>
  
  <p>Ideally I want this ApplicationDbContext to be passed as a parameter to each controller's constructor, like this scenario with dependency injection:</p>
  
  <pre><code>public MessagesController(ApplicationDbContext context){
      this.Db = context;
      this.Mapper = new MessageMapper(Db);
      this.Service = new MessageService();
  }
  </code></pre>
  
  <p>The question is, how to achieve this? I tried to find the source code that controls controller dependency injection, but it comes as .NET dll file and I cannot modify(just like you cannot edit System namespace files). So how is dependency injection possible for controllers in ASP.NET MVC? Can anyone use my scenario as an example? Thanks.</p>