title: Using EF6 with MySQL and SQL Server
body: <p>I have a project using both SQL Server and MySQL database. I followed <a href="https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html" rel="nofollow noreferrer">this post</a> to use MySQL with EF6 and it works perfectly. </p>
  
  <p>Then I added one more <code>DbContext</code> (<code>EfContext</code>) that connects to SQL Server. An exception is raised whenever I execute any command in this context:</p>
  
  <blockquote>
    <p>Unhandled Exception: System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.</p>
    
    <p>MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts.</p>
  </blockquote>
  
  <p>Same problem, no solution</p>
  
  <p><a href="http://stackoverflow.com/questions/23514648/using-mysql-mssql-for-two-different-databases-with-entity-framework">http://stackoverflow.com/questions/23514648/using-mysql-mssql-for-two-different-databases-with-entity-framework</a> </p>
  
  <p><a href="http://stackoverflow.com/questions/21838591/is-it-possible-to-have-a-ef-context-for-mysql-and-sqlserver">http://stackoverflow.com/questions/21838591/is-it-possible-to-have-a-ef-context-for-mysql-and-sqlserver</a> </p>
  
  <p><a href="http://stackoverflow.com/questions/20308378/configure-multiple-database-entity-framework-6">http://stackoverflow.com/questions/20308378/configure-multiple-database-entity-framework-6</a> </p>
  
  <p>I debug and find out the connection of <code>MssqlDbContext</code> is <code>MySql.Data.MySqlClient.MySqlConnection</code> no matter what my provider name is <code>System.Data.SqlClient</code>. It seems <code>MySqlEFConfiguration</code> overrides all config.</p>
  
  <p>Are there any other solutions to my issue?</p>
  
  <p>[Update]</p>
  
  <p>I separated 2 class libraries</p>
  
  <ul>
  <li>SQL Server: only reference EF and EF.SQL (EfContext)</li>
  <li>Mysql: only reference EF and MySql.Data.Entity (MySqlContext)</li>
  </ul>
  
  <p>In demo project</p>
  
  <p>app.config</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="EfContext" 
           connectionString="..." 
           providerName="System.Data.SqlClient" /&gt;
      &lt;add name="MySqlContext" 
           connectionString="..." 
           providerName="MySql.Data.MySqlClient" /&gt;
  &lt;/connectionStrings&gt;
  
  &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
      &lt;providers&gt;
          &lt;provider invariantName="System.Data.SqlClient" 
                    type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
          &lt;provider invariantName="MySql.Data.MySqlClient" 
                    type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" /&gt;
      &lt;/providers&gt;
  &lt;/entityFramework&gt;
  </code></pre>
  
  <p>Case 0: works perfectly if only use one context at one time</p>
  
  <p>Case 1: use EFContext before MySqlContext</p>
  
  <pre><code>var ef = new EfContext("name=EfContext");
  ef.Database.ExecuteSqlCommand("...");
  var mysql = new MySqlContext("name=MySqlContext");
  </code></pre>
  
  <p>Exception: </p>
  
  <blockquote>
    <p>Unhandled Exception: System.TypeInitializationException: The type initializer for 'MySql.Context.MySqlContext' threw an exception.</p>
    
    <p>System.InvalidOperationException: The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered. An instance of 'MySqlEFConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See <a href="http://go.microsoft.com/fwlink/?LinkId=260883" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkId=260883</a> for more information.</p>
  </blockquote>
  
  <p>Case 2: call MySqlContext before EfContext</p>
  
  <pre><code>var mysql = new MySqlContext("name=MySqlContext");
  var ef = new EfContext("name=EfContext");
  ef.Database.ExecuteSqlCommand("...");
  </code></pre>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>Unhandled Exception: System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.</p>
    
    <p>MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. ---> System.AggregateException: One or more errors occurred. ---> System.Net.Sockets.SocketException: No such host is known</p>
  </blockquote>