title: AutoFac / .NET Core - Register DBcontext
body: <p>I have a new .NET Core Web API project that has the following projects structure:</p>
  
  <p>API -> Business / Domain -> Infrastructure</p>
  
  <p>The API is very thin with only the API methods.  The Business / Domain layer has all my business logic.  And finally, my Infrastructure layer has my DB classes using EF Core 2.0.</p>
  
  <p>I know using .NET Core built-in Dependency Injection I can add a reference from the API project to the Infrastructure project, then add the following code in the StartUp.cs file:</p>
  
  <pre><code>services.AddDbContext&lt;MyContext&gt;(options =&gt; options.UseSqlServer(connectionString));
  </code></pre>
  
  <p>However, I would like to maintain a more traditional separation of concerns.  So far I have added a module in my Infrastructure layer that attempts to make the registration like so:</p>
  
  <pre><code>builder.Register(c =&gt;
          {
              var config = c.Resolve&lt;IConfiguration&gt;();
  
              var opt = new DbContextOptionsBuilder&lt;MyContext&gt;();
              opt.UseSqlServer(config.GetSection("ConnectionStrings:MyConnection:ConnectionString").Value);
  
              return new MyContext(opt.Options);
          }).AsImplementedInterfaces().InstancePerLifetimeScope();
  </code></pre>
  
  <p>The DBContext, however, is not getting registered.  Any class that attempts to access the injected DBContext cannot resolve the parameter. </p>
  
  <p><strong>Is there a way to register the DBContext in a separate project using AuftoFac in a .NET Core Web API Project?</strong></p>