Currently you're using DbContext
constructor which accepts either connection string name from web.config or any directly assigned connection string:
public DbContext(
string nameOrConnectionString
)
The latter argument string has major problem which it doesn't provide any way to include database provider name in use (you can't provide providerName
value there, i.e. MySql.Data.MySqlClient
), hence EF automatically selected default provider defined by defaultConnectionFactory
in web.config like this:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
Since the default database provider still being set to SQL Server, it doesn't accept port
as its keyword in connection string, hence throwing ArgumentException
.
To fix this issue, just change default connection factory to MySql.Data.Entity.MySqlConnectionFactory
, so that MySQL is now set as default provider for all connection strings passed to DbContext
(assuming you're using EF 6):
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
</providers>
</entityFramework>
Note: If you provide name of connection string stored inside web.config, it will work since connectionString
element has providerName
attribute which will passed to EF DbContext
:
<add name="MyContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=wordpress;uid=XXXXX;pwd=XXXXX" />
Similar issues:
Dynamic MySQL database connection for Entity Framework 6
Entity Framework defaultConnectionFactory for MySQL