You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core连接远程MySQL数据库报Internal connection fatal error解决方案

ASP.NET Core MVC连接远程MySQL数据库报错解决方案

问题场景

开发ASP.NET Core MVC应用时,在免费SQL服务平台创建了远程数据库,收到的数据库凭据如下:

  • Host: sql6.freesqldatabase.com
  • Database name: sql6455969
  • Database user: sql6455969
  • Database password: pass
  • Port number: 3306

报错信息

连接数据库时触发未处理异常:InvalidOperationException: Internal connection fatal error

初始配置代码

appsettings.json 连接字符串配置

"ConnectionStrings": {
"APDBConnection": "Server = sql6.freesqldatabase.com,3306; Database = sql6455969; User Id = sql6455969; Password = pass;"
 }

Startup.cs 服务注册配置

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("APDBConnection")
        )
    );
    services.AddControllersWithViews();
}

问题根因

使用了SQL Server专属的EF Core驱动连接MySQL数据库,驱动与数据库类型不兼容导致连接报错。

修复方案

  1. 先安装NuGet依赖包 Pomelo.EntityFrameworkCore.MySql
  2. 修改Startup.cs中的DbContext注册逻辑,将UseSqlServer替换为UseMySql即可

修复后Startup.cs代码

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySql(
        Configuration.GetConnectionString("APDBConnection")
    )
);

内容的提问来源于stack exchange,提问作者Heba Atef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 12:39:03