title: Pass parameter to class which inherits from DbContext
body: <p>My application is an <code>ASP.NET Core 1.0 Web API</code>. I am using <code>Entity framework</code>.</p>
  
  <p>I wan't to change the <code>CommandTimeout</code> like this:</p>
  
  <pre><code>public class MyDbContext: DbContext
  {
     private const int Timeout = 180;
  
     public MyDbContext(DbContextOptions options): base(options)
     {
         this.Database.SetCommandTimeout(Timeout);
     }
  }
  </code></pre>
  
  <p>this works if <code>Timeout</code> is defined in my class.</p>
  
  <p>However, I would like to have the value of my <code>Timeout</code> inside my <code>appsettings.json</code> file like this:</p>
  
  <pre><code>  "DbContextSettings": {
      "Timeout": "180"
    }
  </code></pre>
  
  <p>so there are two ways to set the <code>CommandTimeout</code> now:</p>
  
  <ol>
  <li>Pass the <code>CommandTimeOutInSeconds</code> as a parameter to the constructor.</li>
  <li>Get the value out of <code>appsettings.json</code> inside the constructor of my class</li>
  </ol>
  
  <p>I don't know how to achieve one of the ways.</p>
  
  <p>Is this possible? or is there any known workaround? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>Iam never really Initailizing <code>MyDbContext</code>. I do this in <code>ConfigureServices</code> in the <code>Startup</code> class like this: </p>
  
  <pre><code>services.AddDbContext&lt;MyDbContext&gt;(db =&gt; db.UseSqlServer("secret"));
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>@dennisanberlin so let's say I have a controller calling the following method:</p>
  
  <pre><code>public class SomeRepository
  {
  
    private MyDbContext context;
  
    public SomeRepository(MyDbContext myContext)
    {
       this.context = myContext;
    }
  
    public Person SomeMethodCalledByController(){
       return myContext.SomeTableWhichContainsPersons.FirstOrDefault(person =&gt; person.name == "Bob");
    }
  }
  </code></pre>
  
  <p>The class <code>SomeRepository</code> is never getting initialized by me. Iam doing this via <code>dependency injection</code> in the <code>startup</code> like this:</p>
  
  <pre><code>services.AddTransient&lt;SomeRepository&gt;();
  </code></pre>
  
  <p>The class knows about <code>MyDbContext</code> from the above shown line:</p>
  
  <pre><code>services.AddDbContext&lt;MyDbContext&gt;(db =&gt; db.UseSqlServer("secret"));
  </code></pre>
  
  <p>Therefore I never pass an object of <code>MyDbContext</code> directly to any of my classes working with the database.</p>