1

I use MySQL EF 6 (MySQL.Data.Entity package) to connect to MySQL DB. Everything is ok if I use the constructor connectionStringName :base("MyContext"). But it does not work, if i use connectionString directly :base("server=localhost;port=3306;database=wordpress;uid=root"). The application raise error System.ArgumentException: Keyword not supported: 'port'.

My project need use directly connection string. Does anyone know how to fix it? Thank you

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MyContext : DbContext
{
 static MyContext()
 {
 Database.SetInitializer<MyContext>(null);
 }
 public MyContext()
 //:base("server=localhost;port=3306;database=wordpress;uid=root;password=") not work
 :base("MyContext") // it worked if using connectionStringName
 {
 }
}
asked Sep 25, 2017 at 3:09

1 Answer 1

5

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

answered Sep 25, 2017 at 7:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.