title: Using multiple dbcontext instances and dependency injection
body: <p>This is kind of a similar question I asked <a href="https://stackoverflow.com/questions/27669850/setting-the-connection-string-of-a-dbcontext-in-my-repository-class-using-ninjec">here</a> a few weeks ago with one significant change in requirement.</p>
  
  <p>I have a new and unique (I have not found anything like this in my stackoverflow search) business requirement:</p>
  
  <p>I have created two separate entity framework 6 DbContexts that point to two structurally different databases, let us call them PcMaster and PcSubs.  While PcMaster is a straight forward database and the PcMasterContext will have a static connection string, PcSubs database is used as a template to create new databases out of.  Obviously, since these copied databases will have the same exact structure, the idea is to just change the database (catalog) name in the connection string to point to a different db when the dbcontext is instantiated.  I have also used repository pattern and dependency injection (currently Ninject, but thinking of moving to Autofac).</p>
  
  <p>I have not seen an IDbContext interface for DbContext, unless you want to create one yourself.  But then, I have seen many saying that it is not a good idea or not the best practice.</p>
  
  <p>Basically, what I want to do is, under certain conditions, not only the application needs to switch between PCMasterContext and PCSubsContext, but also modify the connection string to PCSubsContext if PCSubsContext is the current context.  the dbContext I used in the repository needs to point to a different database.  I don't know how I can do this with an IoC container such as Ninject or Autofac.  Here are some code snippets I have created so far.  Help with some real working solutions is highly appreciated.</p>
  
  <p>Here is my interface for the base repository</p>
  
  <pre><code>public interface IPCRepositoryBase&lt;T&gt; where T : class
  {
    void Add(T entity);
    void Delete(T entity);
    T FindOne(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
    IQueryable&lt;T&gt; FindBy(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
    IQueryable&lt;T&gt; GetAll();
    void SetConnection(string connString);
    //... 
    //...
  }
  </code></pre>
  
  <p>Here is my abstract repository base</p>
  
  <pre><code>public abstract class PCRepositoryBase&lt;T&gt; : IPCRepositoryBase&lt;T&gt;, IDisposable where T : class
  {
    protected readonly IDbSet&lt;T&gt; dbSet;
    protected DbContext dbCtx;
  
    public PCRepositoryBase(DbContext dbCtx)
    {
       this.dbCtx = dbCtx;
       dbSet = dbCtx.Set&lt;T&gt;();
    }
    public void SetConnection(string connString)
    {
       dbCtx.Database.Connection.ConnectionString = connString;
    }
    public IQueryable&lt;T&gt; FindBy(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
    {
       return dbSet.Where(predicate); // DataContext.Set&lt;T&gt;().Where( predicate  );
    }
    public virtual IQueryable&lt;T&gt; GetAll()
    {
       return dbSet;
    }
    public T FindOne(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
    {
       return dbSet.SingleOrDefault(predicate);
    }
    //... Not all implementations listed
    //...
  }
  </code></pre>
  
  <p>And now, here is the interface for one of the derived repositories:</p>
  
  <pre><code>public interface ISubscriberRepository : IPCRepositoryBase&lt;Subscriber&gt;
  {
    IQueryable&lt;Subscriber&gt; GetByStatusName( PCEnums.enumSubsStatusTypes status   );
    IQueryable&lt;Subscriber&gt; GetByBusinessName( string businessName );
    //...
    //...
  }
  
  public class SubscriberRepository : PCRepositoryBase&lt;Subscriber&gt;, ISubscriberRepository
  {
    public SubscriberRepository( DbContext context ) : base( context ) { }
    public IQueryable&lt;Subscriber&gt; GetByStatusName( PCEnums.enumSubsStatusTypes    status )
    {
       return FindBy(x =&gt; x.SubsStatusType.Name == status.ToString());
    }
    public IQueryable&lt;Subscriber&gt; GetByBusinessName( string businessName )
    {
       return FindBy( s =&gt; s.BusinessName.ToUpper() == businessName.ToUpper()  );
    }
    //... other operations omitted for brevity!
  }
  </code></pre>
  
  <p>Now, my PCSubs dbContext generated by the designer:</p>
  
  <pre><code>public partial class PCSubsDBContext : DbContext
  {
      public PCSubsDBContext() : base("name=PCSubsDBContext")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          throw new UnintentionalCodeFirstException();
      }
  
      public virtual DbSet&lt;Currency&gt; Currencies { get; set; }
      public virtual DbSet&lt;DurationName&gt; DurationNames { get; set; }
      public virtual DbSet&lt;Subscriber&gt; Subscribers { get; set; }
  }
  </code></pre>
  
  <p>Is there a way, I can just use and/or inject one generic dbcontext for both databases along with the connection string for different databases.  How would I register the "DbContext" in the Ioc container without a corresponding interface and still be able to inject the connection string at runtime?  Some code examples will really help.  </p>