title: Difference between AddDbContext and AddDbContextFactory
body: <p>Actually I started with <code>Blazor</code> and <code>EF Core</code>. At registering the <code>DbContext</code> i get stucked. The <code>DbContext</code> can be registerd with <code>AddDbContext</code> or with <code>AddDbContextFactory</code>. But what´s the difference?</p>
  <pre class="lang-cs prettyprint-override"><code>builder.Services.AddDbContext&lt;DataContext&gt;(opt =&gt; opt.UseSqlServer(&quot;...&quot;));
  builder.Services.AddDbContextFactory&lt;DataContext&gt;(opt =&gt; opt.UseSqlServer(&quot;...&quot;));
  </code></pre>
  <p>From docs i got following information:</p>
  <p><code>AddDbContext</code>:</p>
  <blockquote>
  <p>Use this method when using dependency injection ...</p>
  </blockquote>
  <blockquote>
  <p>Entity Framework Core does not support multiple parallel operations being run on the same Microsoft.EntityFrameworkCore.DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads.</p>
  </blockquote>
  <p><code>AddDbContextFactory</code>:</p>
  <blockquote>
  <p>Registering a factory is recommended for Blazor applications and other situations where the dependency injection scope is not aligned with the context lifetime...</p>
  </blockquote>
  <blockquote>
  <p>For convenience, this method also registers the context type itself as a scoped service. This allows a context instance to be resolved from a dependency injection scope directly or created by the factory, as appropriate.</p>
  </blockquote>
  <p>So can we globally say that if the program needs access to <code>DbContext</code> from different threads or at the same time it is necessary to register the context with <code>AddDbContextFactory</code> because when it gets created (e.g. in the controller) the liftime is set to scoped and so we every time get a new <code>DbContext</code>?</p>
  <pre class="lang-cs prettyprint-override"><code>private readonly DataContext _dbContext;
  
  public BlogController(IDbContextFactory&lt;DataContext&gt; dbFactory)
  {
    // Will be created as SCOPED DbContext?
    _dbContext = dbFactory.CreateDbContext();
  }
  </code></pre>
  <p>Also i found a similar Question <a href="https://stackoverflow.com/questions/65022729/use-both-adddbcontextfactory-and-adddbcontext-extension-methods-in-the-same">here</a>. And there the lifetime within <code>AddDbContext</code>and <code>AddDbContextFactory</code> is set during registration. Or I´m missing something.</p>
  <p>So my question generally is:</p>
  <ul>
  <li>When use <code>AddDbContextFactory</code> instead of <code>AddDbContext</code>?</li>
  <li>What´s the lifetime difference from <code>DbContext</code> between <code>DbContextFactory</code> and <code>AddDbContext</code>?</li>
  <li>Should I generally use <code>DbContextFactory</code> for <code>Blazor</code> projects?</li>
  <li>Is there a memory overhead when <code>DbContext</code> is created within a <code>scoped</code> lifetime?</li>
  </ul>