title: EF core unable to resolve environment variable using UseSqlServer with SpecFlow
body: <p>I am converting a BDD test project to the use EF core 5.x and SpecFlow 3.x + Specflow.Autofac. When executing the scenario the context is unable to find the connection string from the environmental variables.</p>
  <p>Solution setup has the following structure</p>
  <ul>
  <li>API (project)</li>
  <li>
  <ul>
  <li>appsettings.json (not the one that should be used for the specflow tests)</li>
  </ul>
  </li>
  <li>Business (project)</li>
  <li>
  <ul>
  <li>BusinessRegister.cs</li>
  </ul>
  </li>
  <li>Data (project)</li>
  <li>
  <ul>
  <li>MyContext.cs</li>
  </ul>
  </li>
  <li>
  <ul>
  <li>DataRegister.cs</li>
  </ul>
  </li>
  <li>BddTest (project)</li>
  <li>
  <ul>
  <li>appsettings.json (has connection string for bdd testing for mycontext)</li>
  </ul>
  </li>
  <li>
  <ul>
  <li>DependencyResolver.cs</li>
  </ul>
  </li>
  <li>
  <ul>
  <li>various scenarios</li>
  </ul>
  </li>
  </ul>
  <p>The scenario dependencies are registered before all scenarios are executed;</p>
  <pre><code>public static class DependencyResolver
  {
      private static IConfiguration config;
  
      [ScenarioDependencies]
      public static ContainerBuilder CreateContainerBuilder()
      {
          var containerBuilder = new ContainerBuilder();
          if (config == null)
          {
              string fullPath = System.Reflection.Assembly.GetAssembly(typeof(DependencyResolver)).Location;
              string theDirectory = Path.GetDirectoryName(fullPath);
  
              config = new ConfigurationBuilder()
                  .AddJsonFile(&quot;appsettings.json&quot;, optional: false, reloadOnChange: true)
                  .SetBasePath(theDirectory)
                  .AddEnvironmentVariables()
                  .Build();
          }
  
          containerBuilder.RegisterInstance(config);
          containerBuilder.RegisterTypes(typeof(DependencyResolver).Assembly.GetTypes().Where(t =&gt; Attribute.IsDefined(t, typeof(BindingAttribute))).ToArray()).SingleInstance(); 
          containerBuilder.RegisterModule(new DependencyRegister());
          containerBuilder.RegisterModule(new BusinessRegister());
          containerBuilder.RegisterModule(new DataRegister());
          return containerBuilder;
      }
  }
  </code></pre>
  <p>The DataRegister registers <code>MyContext</code>;</p>
  <pre><code>public class DataRegister : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.RegisterType&lt;MyContext&gt;().AsSelf().InstancePerDependency();
      }
  }
  </code></pre>
  <p>Inside <code>MyContext</code> it calls <code>UseSqlServer</code> to attempt to resolve a environment variable</p>
  <pre><code>protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
      optionsBuilder.EnableDetailedErrors();
  
      if (!optionsBuilder.IsConfigured)
      {
             optionsBuilder.UseSqlServer(&quot;Name=ConnectionStrings:MyContext&quot;);
      }
  }
  </code></pre>
  <p>The <code>appsettings.json</code> located in the <code>BddTest</code> project</p>
  <pre><code>{
    &quot;Logging&quot;: {
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Information&quot;,
        &quot;Microsoft&quot;: &quot;Warning&quot;,
        &quot;Microsoft.EntityFrameworkCore&quot;: &quot;Debug&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
      }
    },
    &quot;ConnectionStrings&quot;: {
      &quot;MyContext&quot;: &quot;--&quot;
    }
  }
  </code></pre>
  <p>When I attempt to run a scenario the application errors out when attempting to create a database connection inside of <code>MyContext</code>;</p>
  <blockquote>
  <p>System.InvalidOperationException: 'A named connection string was used,
  but the name 'ConnectionStrings:MyContext' was not found in the
  application's configuration. Note that named connection strings are
  only supported when using 'IConfiguration' and a service provider,
  such as in a typical ASP.NET Core application.</p>
  </blockquote>
  <p>How do I register my <code>appsettings.json</code> so that it is available inside the <code>Data</code> project and can be resolved when using <code>optionsBuilder.UseSqlServer(&quot;Name=ConnectionStrings:MyContext&quot;);</code>?</p>