title: How do I get the connection string from the SqlServerDBContextOptionsExtensions in ASP.Net Core
body: <p>I am building up an ASP.Net Core API and I have not been able to find a way to get the connection string from the DBContextOptions.</p>
  
  <p>I have the DBContext in my startup.cs as shown below;</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      // Add framework services.
      services.AddApplicationInsightsTelemetry(Configuration);
  
      services.AddEntityFrameworkSqlServer()
          .AddDbContext&lt;MainContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("MainConnection")));
  
      services.AddMvc()
         .AddJsonOptions(opt =&gt;
          {
              opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
          });
  }
  </code></pre>
  
  <p>And in my Context class, I have the following constructor;</p>
  
  <pre><code>public MainContext(DbContextOptions&lt;MainContext&gt; options) : base(options)
  {
  
  }
  </code></pre>
  
  <p>but it doesn't work unless I add an actual connection string in the DBContext class, OnConfiguring method, as shown below;</p>
  
  <pre><code>protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
      //TODO Move connection string to a secure location
      optionsBuilder.UseSqlServer(@"Server= .....");
  
  }
  </code></pre>
  
  <p>I can see that the Startup.cs is getting the correct connection string from the appsettings.json  file when I debug and examine the value in Configuration.GetConnectionString("MainConnection").</p>
  
  <p>I would think that passing the options into the DbContext class via DI would have passed the connection string but the DbContect class doesn't work unless I have that optionBuilder.UseSqlServer() in the OnConfiguring method.</p>
  
  <p>I found this SO post <a href="https://stackoverflow.com/questions/33532599/asp-net-5-multiple-dbcontext-problems/" title="here">https://stackoverflow.com/questions/33532599/asp-net-5-multiple-dbcontext-problems</a>, that talks about using the following code to extract the connection string from the options property</p>
  
  <pre><code>public ResourceDbContext(DbContextOptions options) : base(options)
  {
      _connectionString = ((SqlServerOptionsExtension)options.Extensions.First()).ConnectionString;
  }
  
  
  protected override void OnConfiguring(DbContextOptionsBuilder options)
  {
      options.UseSqlServer(_connectionString);
  }  
  </code></pre>
  
  <p>But when I try to use it, I find that there is no longer a First() method in options.Extensions</p>
  
  <p>So, my first question is...</p>
  
  <p>Why doesn't the DBContext class work without having to add the connection string in the OnConfiguring method</p>
  
  <p>My second question is ...</p>
  
  <p>If the connection string is required in the OnCOnfiguring method, how can I get it from the DbContextOptions options object rather than  having to explicitly provide it in the  OnConfiguring method --> optionsBuilder.UseSqlServer(@"Server= .....");</p>