I have a project using both SQL Server and MySQL database. I followed this post to use MySQL with EF6 and it works perfectly.
Then I added one more DbContext
(EfContext
) that connects to SQL Server. An exception is raised whenever I execute any command in this context:
Unhandled Exception: System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.
MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts.
Same problem, no solution
http://stackoverflow.com/questions/20308378/configure-multiple-database-entity-framework-6
I debug and find out the connection of MssqlDbContext
is MySql.Data.MySqlClient.MySqlConnection
no matter what my provider name is System.Data.SqlClient
. It seems MySqlEFConfiguration
overrides all config.
Are there any other solutions to my issue?
[Update]
I separated 2 class libraries
- SQL Server: only reference EF and EF.SQL (EfContext)
- Mysql: only reference EF and MySql.Data.Entity (MySqlContext)
In demo project
app.config
<connectionStrings>
<add name="EfContext"
connectionString="..."
providerName="System.Data.SqlClient" />
<add name="MySqlContext"
connectionString="..."
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
Case 0: works perfectly if only use one context at one time
Case 1: use EFContext before MySqlContext
var ef = new EfContext("name=EfContext");
ef.Database.ExecuteSqlCommand("...");
var mysql = new MySqlContext("name=MySqlContext");
Exception:
Unhandled Exception: System.TypeInitializationException: The type initializer for 'MySql.Context.MySqlContext' threw an exception.
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 http://go.microsoft.com/fwlink/?LinkId=260883 for more information.
Case 2: call MySqlContext before EfContext
var mysql = new MySqlContext("name=MySqlContext");
var ef = new EfContext("name=EfContext");
ef.Database.ExecuteSqlCommand("...");
Exception:
Unhandled Exception: System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.
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