title: Dynamic MySQL database connection for Entity Framework 6
body: <p>I wish to pass a dynamic connection string to the entity framework context. I have over 150 schemas which are identical (one per account) and I would like to select the connection as such:</p>
  
  <pre><code>ApplicationDbContext db = new ApplicationDbContext("dbName");
  </code></pre>
  
  <p>In theory this would be fairly easy, as I can create a connectionString and pass it as the argument for the constructor, for example:</p>
  
  <pre><code>public ApplicationDbContext(string dbName) : base(GetConnectionString(dbName))
  {
  }
  
  public static string GetConnectionString(string dbName)
  {
      // The connectionString passed is something like:
      // Server=localhost;Database={0};Uid=username;Pwd=password
      var connString =  ConfigurationManager
                           .ConnectionStrings["MyDatabase"]
                           .ConnectionString
                           .ToString();
  
      return String.Format(connString, dbName);
  }
  </code></pre>
  
  <p>I can connect successfully when I just pass the connection string name, but not when I generate it dynamically as below. I realize now that it's because the connection string in web.config has the <code>providerName="MySql.Data.MySqlClient"</code> attribute in it.</p>
  
  <p>When I pass the actual connection string dynamically to the connection though, it assumes that it needs to connect to SQL Server rather than MySQL and fails due to the connection string being invalid.</p>
  
  <p>The question is, how do I pass the provider name to the connection string if I am creating it dynamically?</p>