title: Register EF Core using IDbContextFactory in ASP.NET Core Mvc
body: <p>I've created a project <code>App.Data</code> that contains domain objects and a base class that implements <code>IDbContextFactory&lt;T&gt;</code> for creating a <code>DbContext</code>. The code is available <a href="https://gist.github.com/joacar/60d6faa9e6e20ba3dfb64b207708f442" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Generating the migrations in a seperate assembly works well. </p>
  
  <p>What I would like to utilize the context I've created in <code>App.Data.Sql</code> that is created via an <code>SqlDbContextFactory&lt;MyAppDbContext&gt;</code> when registering entity framework within asp.net mvc.</p>
  
  <p>What I want to have is something like</p>
  
  <pre><code>services.AddEntityFrameworkSqlServer()
  .AddDbContext(typeof(SqlDbContextFactory&lt;MyAppDbContext&gt;))
  </code></pre>
  
  <p>Registering it as a singleton doesn't work since when resolved complains about no provider has been configured.</p>
  
  <pre><code>services.AddSingleton&lt;IDbContextFactory&lt;..&gt;, SqlDbContextFactory&lt;..&gt;&gt;();
  services.AddScoped&lt;MyApp..&gt;(p =&gt; p.GetRequiredService&lt;IDb..&gt;().Create());
  </code></pre>