How To connect to mysql database with EntityFramework Core?

0

In the goal to use asp.net core mvc with the mysql database , i have downloaded the specific provider of EF_Core for Mysql.

Then i registred the DbContext service in the startup file:

 services.AddDbContext<NawrasContext>(options=>
               options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

and this is my appsettings.json :

  {
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Server=centreenmi2019.mysql.db;Database=centreenmi2019;Uid=centreenmi2019;Pwd=Bced75652102;"

  }
}

I have successfully added my first migration , but when i try to update the database , i get this error :

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
  An error occurred using the connection to database '' on server 'centreenmi2019.mysql.db'.
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
 at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 440 

what i m doing wrong ? why the error is telling me :

An error occurred using the connection to database '' on server 'centreenmi2019.mysql.db'. while the name of the database in the connection string is specified ?

mysql
entity-framework-core
connection-string
asked on Stack Overflow Oct 23, 2019 by A.HADDAD • edited Oct 23, 2019 by Marc Delisle

1 Answer

0

Using these steps you can solve the issue

  1. Use a Nugat package named "Pomelo.EntityFrameworkCore.MySql"
  2. Register service to Startup.cs

    services.AddCors();

    services.AddDbContext(options =>options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

  3. Use this connection string in appsettings.json

{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } },

"ConnectionStrings": { "DefaultConnection": "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; Encrypt=true;" } }

answered on Stack Overflow Oct 25, 2019 by Abdullah Al Mamun Shiham • edited Oct 25, 2019 by Abdullah Al Mamun Shiham

User contributions licensed under CC BY-SA 3.0