title: ASP.NET CORE DI in model
body: <p>I'm trying to understand how DI works in the new ASP Core. With a tutorial, I made it work for controllers, but can't make it work for models. For example, I have an AuthController, and I injected the database context to it, but now, as I have more controllers, sharing the same model, which is <code>Authentication</code>, I'd like to inject the context to the model itself. Here's some piece of code I've: </p>
  
  <p>From the <code>Startup.cs</code></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      ...
      services.AddDbContext&lt;GameContext&gt;(options =&gt; options.UseSqlServer(@"Data Source=DESKTOP-USER\SQLEXPRESS;Initial Catalog=Db7;Integrated Security=True;Connect Timeout=30;"));
  }
  </code></pre>
  
  <p>And here's how I used it from controllers: </p>
  
  <pre><code>[Route("api/[controller]")]
  public class AuthController : Controller
  {
      public GameContext db;
      public AuthController(GameContext context)
      {
          db = context;
      }
  
      [HttpPost]
      [Route("login")]
      public LoginResponseModel Login([FromBody] LoginModel user) //public Models.VM.LoginModel Login([FromBody] Models.VM.LoginModel user)
      {
          //query user
          var detectedUser = db.Users.FirstOrDefault(u =&gt; u.Email == user.Email &amp;&amp; u.Password == HelperClass.Md5(user.Password);
  </code></pre>
  
  <p>If I remove the context part from the controller, and move it to the model, I won't be able to instatntiate it again as the constructor needs an argument (which would be injected automatically?)</p>
  
  <pre><code>public class Authentication
  {
      public GameContext db;
  
      public Authentication(GameContext context)
      {
          db = context;
      }
      ...
  </code></pre>
  
  <p>How could I reach the database from the model? </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This is how my Authentication class would look like (constructor may look different based on the solution):</p>
  
  <pre><code>public class Authentication
  {
      public GameContext db;
  
      public Authentication(GameContext context)
      {
          db = context;
      }
  
      public LoginResponseModel Login(LoginModel user)
      {
          //query user
          var detectedUser = db.Users.FirstOrDefault(u =&gt; u.Email == user.Email &amp;&amp; u.Password == HelperClass.Md5(user.Password));
  </code></pre>
  
  <p>And here's how I would like to use this model from controllers: </p>
  
  <pre><code>[Route("api/[controller]")]
  public class AuthController : Controller
  {
  
      public AuthController(GameContext context)
      {
      }
  
      // POST api/login
      [HttpPost]
      [Route("login")]
      public LoginResponseModel Login([FromBody] LoginModel user) //public Models.VM.LoginModel Login([FromBody] Models.VM.LoginModel user)
      {
          Authentication auth = new Authentication(); //throws error since no parameter passed
  
          return auth.Login(user);
      }
  </code></pre>