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

≯℡__Kan透↙ 提交于 2020-05-17 08:45:07

问题


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://docs.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;"
  }

回答1:


Your connection string doesn’t seem to be right or EF is not able to pull it. You ll need to check the docs and select the correct project before running the update. Confirm the connection string using this post:

https://www.c-sharpcorner.com/UploadFile/suthish_nair/how-to-generate-or-find-connection-string-from-visual-studio/

You can try this workaround to specify the connection with the command, PS you ll need to update the provider name for MySql:

Update-Database -Verbose 
 -ConnectionString "CONNECTIONSTRING" 
 -ConnectionProviderName "System.Data.SqlClient"
 -StartupProjectName WEBSITE_PROJECT -ProjectName MIGRATION_PROJECT



回答2:


You can try this way

  1. Install package Pomelo.EntityFrameworkCore.MySQL

  2. Add services at Startup.cs

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

  3. change connection string at appsettings.json

    "ConnectionStrings": { "DatabaseConnectionString": "server=localhost;port=3306;database=MyAppDB;user=root;password=" }

    *change the port number according to your MySQL server

    4.Run these commads at Package Manager Console for data migration Add-Migration InitialCreate

    Update-Database

You can look at the project at github, for better understanding



来源:https://stackoverflow.com/questions/61427947/entity-framework-core-error-an-error-occurred-using-the-connection-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!