Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework to MySql connection failed to database [duplicate]

I am getting below error while connection to mysql database using Entity Framework 6

An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.

Here is my code:

public class TestDbContext : DbContext
{
    public TestDbContext()
       : base("name="mysqldb")
    {

    }
    public DbSet<holidays> holidays { get; set; }
}


<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />   </configSections>

<connectionStrings>
<add name="mysqldb" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;userid=root;password=admin;database=bobdata;/> </connectionStrings>

<entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
    <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, Version=6.10.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>

In inner exception below message given

{"Attempt by method 'MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(System.Data.Common.DbConnection)' to access method 'MySql.Data.MySqlClient.MySqlConnection.get_Settings()' failed."}

like image 695
Dhiraj Mane Avatar asked Jun 03 '26 22:06

Dhiraj Mane


1 Answers

You appear to be using MySql.Data.Entity 6.10.7. I'm going to guess you're also using MySql.Data 8.0.11.

These two libraries are not compatible. Oracle renamed the package to MySql.Data.EntityFramework for v8.x. You need to uninstall MySql.Data.Entity and install MySql.Data.EntityFramework.

like image 74
Bradley Grainger Avatar answered Jun 05 '26 13:06

Bradley Grainger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!