title: EF6 DBContext Dynamic Connection String
body: <pre><code>public partial class ProcessContext : DbContext
  {
      static ProcessContext()
      {
          Database.SetInitializer&lt;ProcessContext&gt;(null);
      }
  
      public ProcessContext()
          : base("Name=ProcessCS") //Comes from Config File
      {
      }
  
      --DBSets 
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
         --Code
      }
  }
  </code></pre>
  
  <p>This is a Multi Tenent DB where we have 3 Different DB's. Centralized DB is in common location and would not be changed. This is where rest of the DB details will be stored. I need to create the Connection string @ runtime where the details will be coming from this centralized DB. Can some one please let me know how to go about it?</p>
  
  <p>I tried with the following code, but it is not working. This Method will be called here</p>
  
  <pre><code>public ProcessContext()
      : base(nameOrConnectionString: ConnectionString())
  {
  }
  
  private static string ConnectionString()
  {
      SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
      sqlBuilder.DataSource = "XXX";
      sqlBuilder.InitialCatalog = "YYY";
      sqlBuilder.PersistSecurityInfo = true;
      sqlBuilder.IntegratedSecurity = true;
      sqlBuilder.MultipleActiveResultSets = true;
  
      EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
      entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
      entityBuilder.Metadata = "res://*/";
      entityBuilder.Provider = "System.Data.SqlClient";
  
      return entityBuilder.ToString();
  }
  </code></pre>