title: How to inject dependencies into models in asp.net core?
body: <p>Let's say I have a controller action that looks like this:</p>
  
  <pre><code>[HttpPost]
  public async Task&lt;IActionResult&gt; Add([FromBody] MyModel model){
      await model.Save();
      return CreatedAtRoute("GetModel", new {id = model.Id}, model);
  }
  </code></pre>
  
  <p>In order to get <code>model.Save</code> to work, it needs some dependencies:</p>
  
  <pre><code>public class MyModel{
      private readonly ApplicationDbContext _context;
      public MyModel(ApplicationDbContext context){
          _context = context;
      }
      public async Task Save(){
          // Do something with _context;
      }
  }
  </code></pre>
  
  <p>As of right now, context is <code>null</code> in <code>MyModel</code>'s constructor. How can I inject it? I'm aware that I can inject services into the controller and perform operations on my model that way, but what if I'd rather use an object-oriented approach than an anemic domain model? Is it simply impossible?</p>