title: Same application, different databases: Entity framework 6.X + MySQL + SQL Server
body: <p>Yesterday I did migration (EF 5.0 => EF 6.0) of web application that uses entity framework to reach MySql and SQL Server databases (particular <code>DbContext</code> to particular databases, NOT any <code>DbContext</code> to any type of database).</p>
  
  <p>Compile time things were done without any issues, run-time faced me with exception: </p>
  
  <blockquote>
    <p>The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered.</p>
  </blockquote>
  
  <p>The <code>[DbConfigurationType(typeof(MySqlEFConfiguration))]</code> attribute on the context appears to have been ignored at run time because the context is in an external assembly(?) and the <code>DbConfiguration</code> used instead is global to the application domain, not the specific to the context(?)."</p>
  
  <p>I tried different approaches to fix it, then googled it and - surprise - find no working solution.</p>
  
  <p>Looks like situation described well formed here <a href="http://forums.mysql.com/read.php?174,614148,614148" rel="noreferrer">http://forums.mysql.com/read.php?174,614148,614148</a> still not changed, or I missed some obvious things.</p>
  
  <p>Any feedback will be appreciated. </p>
  
  <p>Thank you in advance!</p>
  
  <p><strong><em>DETAILED DESCRIPTION:</em></strong></p>
  
  <p>Input (simplified):
  - ASP.NET Web Application</p>
  
  <ul>
  <li><p>Data access layer implemented over Entity Framework 6.1.1</p></li>
  <li><p>Entity Framework providers:</p>
  
  <ul>
  <li><p>System.Data.SqlClient 6.1.1</p></li>
  <li><p>MySql.Data.MySqlClient 6.9.4</p></li>
  </ul></li>
  <li><p>MY_SqlContext, model first concept, targeted to MY SQL Server database</p></li>
  <li><p>Ms_SqlContext, database first concept, targeted to MS SQL Server database</p></li>
  </ul>
  
  <p>According generic documentation of Entity Framework 6 and MySql Connector/Net documentation 
  (<a href="http://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html" rel="noreferrer">http://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html</a>),
  MY_SqlContext requires MySqlEFConfiguration to be applied.</p>
  
  <p>According both documentations, refered above, there are three options to do that.
  All three was tried and failed.</p>
  
  <p><strong>Option 1</strong>: Adding the DbConfigurationTypeAttribute <code>[DbConfigurationType(typeof(MySqlEFConfiguration))]</code> to MY_SqlContext class</p>
  
  <p>Appropriate Web.config segments:</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="MY_SqlContext"
           connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
           providerName="MySql.Data.MySqlClient" /&gt;
      &lt;add name="Ms_SqlContext"
           connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&amp;quot;"
           providerName="System.Data.EntityClient" /&gt;
  &lt;/connectionStrings&gt;
  
  &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
      &lt;providers&gt;
          &lt;provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,  MySql.Data.Entity.EF6" /&gt;
          &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
  &lt;/entityFramework&gt;
  </code></pre>
  
  <p>After application starts and web requests begin processing:</p>
  
  <p>Ms_SqlContext works fine but trying to create a MY_SqlContext instance, I get the exception:</p>
  
  <blockquote>
    <p>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 ...LinkId=260883 for more information."</p>
  </blockquote>
  
  <p><strong>Option 2</strong>: Calling DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) at the application startup</p>
  
  <p>Appropriate Web.config segments (same as Option 1, actually):</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="MY_SqlContext"
           connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
           providerName="MySql.Data.MySqlClient" /&gt;
      &lt;add name="Ms_SqlContext"
           connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&amp;quot;"
           providerName="System.Data.EntityClient" /&gt;
  &lt;/connectionStrings&gt;
  
  &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
      &lt;providers&gt;
          &lt;provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,  MySql.Data.Entity.EF6" /&gt;
          &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
  &lt;/entityFramework&gt;
  </code></pre>
  
  <p>Code, added to Global.asax.cs:
          private void Application_Start(object sender, EventArgs e)
          {
              DbConfiguration.SetConfiguration(new MySqlEFConfiguration());
              ...</p>
  
  <p>After application starts and web requests begin processing trying to create a Ms_SqlContext instance, I get the exception:</p>
  
  <blockquote>
    <p>An instance of 'MySqlEFConfiguration' was set but this type was not discovered in the same assembly as the 'Ms_SqlContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file. See ...?LinkId=260883 for more information.</p>
  </blockquote>
  
  <p><strong>Option 3</strong>: Set the DbConfiguration type in the configuration file</p>
  
  <p>Appropriate Web.config segments </p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="MY_SqlContext"
           connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
           providerName="MySql.Data.MySqlClient" /&gt;
      &lt;add name="Ms_SqlContext"
           connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&amp;quot;"
           providerName="System.Data.EntityClient" /&gt;
  &lt;/connectionStrings&gt;
  
  &lt;entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6"&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
      &lt;providers&gt;
          &lt;provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,  MySql.Data.Entity.EF6" /&gt;
          &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
  &lt;/entityFramework&gt;
  </code></pre>
  
  <p>After application starts and web requests begin processing:
              ...
  Ms_SqlContext instance is created, but during first query execution,  I get the exception:</p>
  
  <blockquote>
    <p>EntityException: {"The underlying provider failed on Open."}</p>
    
    <p>InnerException: {"Unable to connect to any of the specified MySQL hosts."}</p>
  </blockquote>
  
  <p>So, Ms_SqlContext get MySql configuration that is obviously wrong.</p>