title: AddDbContext not working in ConfigureServices of ASP.NET Razor 2.2
body: <p>I reversed a sql server database into their respective classes and context, and all this appears to of worked fine.</p>
  
  <p>In the min context class, there is a line of code pointing to the connection string, which if left in works fine, but the connection string is fully embedded in the context file.</p>
  
  <p>I tried commenting it out of the context and adding via the <code>ConfigureServices</code> section (so I can use the <code>conn</code> string in <em>appsettings.json</em>) using </p>
  
  <pre><code>services.AddDbContext&lt;myDBContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("dbconn")));
  </code></pre>
  
  <p>But when I run, I get an error:-</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'No database provider has been
    configured for this DbContext. A provider can be configured by
    overriding the DbContext.OnConfiguring method or by using AddDbContext
    on the application service provider. If AddDbContext is used, then
    also ensure that your DbContext type accepts a
    <code>DbContextOptions&lt;TContext&gt;</code> object in its constructor and passes it to
    the base constructor for DbContext.'</p>
  </blockquote>
  
  <p>I have tried numerous things on Google but I always end up with the same error, unless I uncomment the hard-coded connection string in the context class file.</p>
  
  <p>Any idea what I am doing wrong?</p>
  
  <p>I have tried many many Google responses and all have the same error - the:-</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'No database provider has been
    configured for this DbContext. A provider can be configured by
    overriding the DbContext.OnConfiguring method or by using AddDbContext
    on the application service provider. If AddDbContext is used, then
    also ensure that your DbContext type accepts a
    <code>DbContextOptions&lt;TContext&gt;</code> object in its constructor and passes it to
    the base constructor for DbContext.'</p>
  </blockquote>
  
  <p>I comment this out from the context class:-</p>
  
  <pre><code>optionsBuilder.UseSqlServer("Server=MYSERVER\\MSSQL2017EXPRESS;Database=DBNAME;Trusted_Connection=True;user id=DBUSER;password=DBPASSWORD;");
  </code></pre>
  
  <p>And this is what is in startup.cs but not working:-</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
      {
          options.CheckConsentNeeded = context =&gt; true;
          options.MinimumSameSitePolicy = SameSiteMode.None;
      });
     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  
      //My DB conn
      services.AddDbContext&lt;myDatabaseContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("dbconn")));
  
  }
  </code></pre>
  
  <p>I need to be able to provide the connection string from <em>appsettings.json</em> and not have it hard coded in to the context class file.</p>