title: How to pass connection string from appsettings.[name].json to DbContext in .NET Core?
body: <p>I have a .net core app. I write my connection string in appsettings.json file.
  But, now, I want to get that connection string for my context. How to do that?</p>
  
  <p>In .net framework 4.6 I used this:</p>
  
  <pre><code>ConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString;
  </code></pre>
  
  <p>I have appsettings.Development.json like this (*** could be any name):</p>
  
  <pre><code>"ConnectionStrings": {
      "***": "Server=***;Database=****;Trusted_Connection=True;MultipleActiveResultSets=true;"
    }
  </code></pre>
  
  <p>I added service:</p>
  
  <pre><code>services.AddTransient&lt;MyContext&gt;(provider =&gt;
              {
                  return new MyContext(configuration["ConnectionStrings:***"]);
              });
  </code></pre>
  
  <p>and this is context constructor (ONLY THIS, not default ones because if I write a default one doesn't take my connection string from json file):</p>
  
  <pre><code>public MyContext(string connectionString)
          : base(connectionString)
      {
          try
          {
              this.Database.Log = (s) =&gt; System.Diagnostics.Debug.Write(s);
          }
          catch (Exception e)
          {
              //CurrentLogger.Log.Error(e);
          }
      }
  </code></pre>
  
  <p>After that I've got this error after I run Enable-Migration command</p>
  
  <p><em>The target context 'MyContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.</em></p>
  
  <p>If somehow I could get connection string from json here:</p>
  
  <p><a href="https://i.stack.imgur.com/D4JTw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D4JTw.jpg" alt="enter image description here"></a></p>
  
  <p>all will be good. But I want to do that with a single line in <strong>.NET CORE</strong> if it would be possible, like in .net framework: with this: </p>
  
  <pre><code>ConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString;
  </code></pre>
  
  <p><strong>.net framework 4.6</strong></p>
  
  <p><a href="https://i.stack.imgur.com/uHETS.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uHETS.jpg" alt="enter image description here"></a></p>