title: EF core DbContext with dependency injection
body: <p>I've created an injectable dbcontext</p>
  <p>Startup.cs:</p>
  <pre><code>    public void ConfigureServices(IServiceCollection services) {
          services.AddScoped&lt;IUnitOfWork, UnitOfWork&gt;();
  
          services.AddDbContext&lt;DBContext&gt;(options =&gt; options.UseSqlServer(&quot;Server=localhost;Database=mydb;Trusted_Connection=True;&quot;));
      }
  </code></pre>
  <p>UnitOfWork:</p>
  <pre><code> public class UnitOfWork : IUnitOfWork {
      private readonly DBContext _context;
      public UnitOfWork(DBContext context) {
          _context = context;
      }
  </code></pre>
  <p>The injection working fine in the controller:</p>
  <pre><code>  public class UserController : ControllerBase {
      private readonly IUnitOfWork unitOfWork;
      public UserController(IUnitOfWork unitOfWork) {
          this.unitOfWork = unitOfWork;
      }
    }
  </code></pre>
  <p>How can I create custom class that takes IUnitOfWork in the constructor and call it from main program?</p>
  <p>CustomClass :</p>
  <pre><code>  public class CustomClass {
      private readonly IUnitOfWork unitOfWork;
      public CustomClass(IUnitOfWork unitOfWork) {
          this.unitOfWork = unitOfWork;
      }
  }
  </code></pre>
  <p>Main program:</p>
  <pre><code>    public class Program {
      public static void Main(string[] args) {
          var unitOfWork=new UnitOfWork()  // Here I don't want to pass new DBContext I want to reach the same injected DBContext 
          var customClass =new CustomClass (unitOfWork);
  
      }
  }
  </code></pre>