title: dotnet muli db context
body: <p>In my task (dotnet core, c#), it is necessary to select one of the databases and make some kind of manipulation in accordance with the query.</p>
  
  <p>According with microsoft docs, it's look like:</p>
  
  <pre><code>public class db1 : DbContext, Idb
  {
      public db1(DbContextOptions options) : base(options)
      {}
  }
  
  public class db2 : DbContext, Idb
  {
      public db2(DbContextOptions options) : base(options)
      {}
  }
  </code></pre>
  
  <p>In Startup.cs</p>
  
  <pre><code>services.AddDbContext&lt;db1&gt;(options =&gt;
      options.UseSqlServer(Configuration.GetConnectionString("db1")));
  
  services.AddDbContext&lt;db2&gt;(options =&gt;
      options.UseSqlServer(Configuration.GetConnectionString("db2")));
  </code></pre>
  
  <p>This allows you to register in DI and access a specific database connection instance, but all databases are hardcoded. This is a poor code. How is it better to make registration in DI by id of a database and select from DI's service by this id in a controller?</p>