title: How do I obtain the IConfiguration parameter to instantiate a EF DbContext?
body: <p>I need to instantiate the following DbContext class:</p>
  <pre class="lang-cs prettyprint-override"><code>public NWBContext(DbContextOptions&lt;NWBContext&gt; options, IConfiguration config) : base(options) {
      Config = config;
  }
  </code></pre>
  <p>My current code to instantiate an instance of <code>NWBContext</code> is below.</p>
  <pre class="lang-cs prettyprint-override"><code>string connectionString = connString;
  
  var optionsBuilder = new DbContextOptionsBuilder&lt;NWBContext&gt;();
  optionsBuilder.UseSqlServer(connectionString);
  
  var config = new ConfigurationBuilder()                
  .AddJsonFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,&quot;appsettings.json&quot;))
  .Build();
  
  using (var dBContext = new NWBContext(optionsBuilder.Options, (IConfiguration)config)) {
  
  }
  </code></pre>
  <p>This is for a class to import data from a CSV file into SQL. The import is too granular for a bulk insert and involves calculations that would be difficult in T-SQL.</p>
  <p>The documentation at <a href="https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/#simple-dbcontext-initialization-with-new" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/#simple-dbcontext-initialization-with-new</a> shows the following example:</p>
  <pre class="lang-cs prettyprint-override"><code>var contextOptions = new DbContextOptionsBuilder&lt;ApplicationDbContext&gt;()
      .UseSqlServer(@&quot;Server=(localdb)\mssqllocaldb;Database=Test&quot;)
      .Options;
  
  using var context = new ApplicationDbContext(contextOptions);
  </code></pre>
  <p><strong>The example does not address an <code>IConfiguration</code> parameter.</strong></p>
  <p>How would I instantiate the <code>IConfiguration</code> parameter to pass to the <code>NWBContext</code> constructor?</p>
  <p>Some documentation implies that <code>IConfiguration</code> could be obtained from <code>services</code>. How would I obtain <code>services</code>?</p>