Question

I'm trying to connect an mvc website I created with Visual Studio to MySQL. My website worked just fine when using SqlServer.

In my Web.config I added:

<connectionStrings>
    <add name="MySqlConnection" connectionString ="Server=localhost;port=3306;Database=BachelorParty;Uid=root;Pwd=root;"  providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>

There are no problems logging in with user 'root' and password 'root' in MySQL Workbench.

In my DbContext class I added:

[DbConfigurationType(typeof(MySqlEFConfiguration))]

When I run the whole thing I get a nullreferenceexception in global.asax:

Database.SetInitializer(new BachelorPartyInitializer());
new BachelorPartyContext().Students.ToList(); //<--nullreferenceexception

On refreshing the server explorer the following happens:

Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: 
Access denied for user 'root'@'localhost' (using password: NO)

Any ideas on fixing this demoralizing issue? EntityFramework, MySql.Data, MySql.Data.Entities, MySql Connector etc. are installed...

Was it helpful?

Solution 3

Meanwhile I fixed my problem, I had to exclude 'port=3306' from my connectionString

<connectionStrings>
<add name="BachelorProef" connectionString="Server=localhost;Database=BachelorParty;Uid=root;Pwd=root;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

OTHER TIPS

Have you tried different connection string format?

<add name="MySqlConnection" 
     connectionString="server=localhost;User Id=root;password=root;Persist Security Info=True;database=BachelorParty;port=3306;" 
     providerName="MySql.Data.MySqlClient" />

OK I had this same problem and solved as follows:

MySql.Data.MySqlClient.MySqlConnection msc=new MySql.Data.MySqlClient.MySqlConnection(myConnectionstring);

System.Data.Entity.DbContext tE1 = new System.Data.Entity.DbContext(msc, false);

Then use tE1 to work with your data....tE1 can also be an EF6 context with all your typed datasets and so on...........

The important thing seems to be to override the default DbContext connection with a MySQL specific connection. It is a mistake to use an MySQL connection string with the default context connection, even though this "mistake" seems to work fine for most purposes.

Jeltz

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top