title: Dependency Injection in Model classes (entities)
body: <p>I am building an ASP.NET Core MVC application with Entity Framework Code-First.
  I implemented a simple repository pattern, providing basic CRUD operations for all the model classes I have created.
  I chose to follow all the recommendations provided in <a href="http://docs.asp.net" rel="nofollow noreferrer">docs</a> and DI is one of these.</p>
  <p>In ~~.NET 5~~ (<em>6 years later update: .net 5 was the alpha name of .net core 1.0</em>) dependency injection works very well for any class that we do not directly instantiate (e.g.: controllers, data repositories, ...).</p>
  <p>We simply inject them via the constructor, and register the mappings in the Startup class of the application :</p>
  <pre class="lang-cs prettyprint-override"><code>// Some repository class
  public class MyRepository : IMyRepository
  {
      private readonly IMyDependency _myDependency;
      public MyRepository(IMyDependency myDependency)
      {
          _myDependency = myDependency;
      }
  }
  
  // In startup.cs :
  services.AddScoped&lt;IMyDependency, MyDependency&gt;();
  services.AddScoped&lt;IMyRepository, MyRepository&gt;();
  </code></pre>
  <p>The problem is that in some of my model classes, I would like to inject some of the dependencies I have declared.</p>
  <p>But I think that I cannot use the constructor injection pattern because model classes are often explicitly instantiated. Therefore, I would need to provide myself with the dependencies, which I can't.</p>
  <p>So my question is: is there another way than constructor injection to inject dependencies, and how? I was for example thinking of an attribute pattern or something like that.</p>