2

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."}

0

3 Answers 3

20

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.

3
  • Thanks, this blocked be for months, had to use old versions of MySql.Data and MySql.Data.Entity.
    – deerchao
    Commented Sep 17, 2018 at 3:53
  • 1
    That was excellent, man
    – megz
    Commented Nov 6, 2018 at 18:08
  • Perfect Solution! That was wrong package and it's working now, Thank you :-) Commented Feb 9, 2020 at 4:37
-1

I think the issue is because you used userid instead of UID in your connection string, change userid to UID

1
  • thanks for quick replay. I tried with UID also but it not works. Commented May 3, 2018 at 15:18
-1

Based on the information you have given (there are other things that can cause this) and assuming the credentials are correct, it would be worth trying this:

In TestDbContext class:

public class TestDbContext : DbContext
{
    public TestDbContext()
       : base("mysqldb")
    {  
    }

    public static TestDbContext Create()
    {
        return new TestDbContext();
    }

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

And in web.config:

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

Update based on comments:

If you are still getting the error, I would recommend starting a new project just to establish a successful database connection and amend your connection string incrementally from there. Then you can replicate established set up in this project to your existing project. Make sure that the connection string name in the db context is the same as in web.config, and start out with a "standard" connection string, like this:

public class TestDbContext : DbContext
{
    public TestDbContext() : base("ThisIsMyConnectionStringName")
    {  
    }

    public static TestDbContext Create()
    {
        return new TestDbContext();
    }

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

  <connectionStrings>
    <add name="ThisIsMyConnectionStringName" connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\My-Database-Name.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

The database file (named My-Database-Name.mdf) will reside in the App_Data folder using this set up. Then search for connection string set-ups out there (there's plenty of resources) and incrementally change things to what you want. This way, you're moving from a place where you actually have a database, and a connection, as a starting point rather than trying to work backwards from an error message which could be caused by a number of things.

3
  • After doing above changes, get same error "An error occurred accessing the database." Commented May 4, 2018 at 6:20
  • Do I need to Install MySQL For Visual Studio Commented May 4, 2018 at 8:36
  • @Dhiraj Mane updated answer with suggestion
    – RickL
    Commented May 4, 2018 at 14:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.