Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core Error: An error occurred using the connection to database '' on server 'localhost'

I have made an ASP.NET Core 3.1 Web-based application and want to use MySQL as the database.

I have been following along with some YouTube tutorials on creating MySQL database with ASP.NET Core 3.1 [code first approach] including a tutorial from this site:

https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-3.1&tabs=visual-studio

I have created a DataModel Class, added a service to UseMySQL to the Startup.cs Class and created an AppDBContext Class that implements DbContext Class.

When I run this command in the Package Manager Console: Add-Migration InitialDatabase the application is creating a migration successfully.

When I run update-database it is throwing this exception:

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
      An error occurred using the connection to database '' on server 'localhost'.
An error occurred using the connection to database '' on server 'localhost'.
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySQL' call.

When I call the EnableRetryOnFailure(); function as required, I am facing this exception:

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004] An error occurred using the connection to database '' on server 'localhost'. An error occurred using the connection to database '' on server 'localhost'.

What could be the issue? Where am I getting it wrong?

If you have links to useful articles about using MySQL Database with ASP.NET Core I would appreciate or your help on this particular issue.

I am using Visual Studio IDE 2019 and ASP.NET Core 3.1.1

Additional Code:

This is the Startup.cs Class:

private IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
    _configuration = configuration;
}

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    // database connection string configuration  
    services.AddDbContextPool<AppDBContext>(options => options.UseMySql(_configuration.GetConnectionString("DatabaseConnectionString"),
        mySqlOptionsAction: options => { options.EnableRetryOnFailure(); }
        ));

    services.AddMvc();
}

This is the connection string in appsettings.json:

  "ConnectionStrings": {
    "DatabaseConnectionString": "Server=localhost;Database=MyAppDB;user=root;Password=123;"
  }
like image 875
Russell Chidhakwa Avatar asked May 16 '26 10:05

Russell Chidhakwa


2 Answers

In my case, I just added the database port separately.

changed this from

MySQL": "server=mysqlserver.com:3306;user=db_user;password=db_pass;database=database_name"

to

MySQL": "server=mysqlserver.com;port=3306;user=db_user;password=db_pass;database=database_name"
like image 182
Muhammad Fuzail Zubari Avatar answered May 17 '26 23:05

Muhammad Fuzail Zubari


I think I found the root of the problem. This error also occurs when the connection to the database couldn't have been established. Make sure the information in your connection string is correct. This error message is very misleading, I spent couple of hours figuring this out because of it.

like image 45
Dominik Brych Avatar answered May 18 '26 00:05

Dominik Brych



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!