实体框架核心错误:使用服务器“ localhost”上的数据库“”连接时发生错误 [英] Entity Framework Core Error: An error occurred using the connection to database '' on server 'localhost'

查看:899
本文介绍了实体框架核心错误:使用服务器“ localhost”上的数据库“”连接时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个 ASP.NET Core 3.1 基于Web的应用程序,并希望使用MySQL作为数据库。

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

我一直在跟着一些YouTube教程一起学习如何使用 ASP.NET Core 3.1 创建 MySQL数据库 > [代码优先方法] 包括该站点的教程:

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/zh-cn/aspnet/core /data/ef-rp/intro?view=aspnetcore-3.1&tabs=visual-studio

我创建了 DataModel类,向 UseMySQL 的Startup.cs类中添加了服务,并创建了 AppDBContext类实现 DbContext类

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.

当我在 Package Manager控制台中运行此命令时 Add-Migration InitialDatabase 应用程序正在创建迁移成功

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

当我运行 update-database 时,它抛出此异常:

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.

当我调用 EnableRetryOnFailure(); 函数按要求运行,我遇到了这个异常:

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


失败:Microsoft.EntityFrameworkCore.Database.Connection [20004]
一个错误使用与服务器 localhost上的数据库的连接发生。使用
服务器‘localhost’上的数据库’连接时发生错误。

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?

如果您有有关使用 MySQL数据库的有用文章的链接对于 ASP.NET Core ,我们将不胜感激,或者您对此特定问题的帮助。

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.

我正在使用 Visual Studio IDE 2019 ASP.NET Core 3.1.1

附加代码:

这是Startup.cs类:

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();
}


这是连接字符串在appsettings.json中:


  "ConnectionStrings": {
    "DatabaseConnectionString": "Server=localhost;Database=MyAppDB;user=root;Password=123;"
  }



推荐答案

<我想我找到了问题的根源。如果无法建立与数据库的连接,也会发生此错误。确保连接字符串中的信息正确。此错误消息非常容易引起误解,因此我花了几个小时才弄清楚这一点。

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.

这篇关于实体框架核心错误:使用服务器“ localhost”上的数据库“”连接时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆