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

C# .NET Core 5.0发布版应用连接MySQL数据库报错问题排查

Hey Paulo, let's break down what's going on here and fix that frustrating post-publish error.

First off, even though you mentioned not installing EntityFrameworkCore, your Startup code tells a different story: AddDbContext<UserDbContext>, options.UseMySQL(conString), and AddEntityFrameworkStores<UserDbContext> all mean you're relying on MySql.EntityFrameworkCore (which pulls in Microsoft.EntityFrameworkCore dependencies automatically). That's key context for the error.

The Root Cause

The critical line in your error log reveals the core issue:

System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.

Older versions of the MySql.Data library (used under the hood by MySql.EntityFrameworkCore) depend on System.Configuration to read settings. When you publish your app as a single-file bundle, the Assembly.CodeBase API it uses isn't supported in that environment—this is a known compatibility gap between older MySQL EF Core packages and .NET Core's single-file publishing.

Solutions & Troubleshooting Steps

1. Upgrade Your MySQL NuGet Packages (Recommended Fix)

The simplest and most permanent fix is updating your MySQL-related packages to the latest stable versions (8.0.33 or newer). These releases have patched the single-file bundle compatibility issue:

  • Open your NuGet Package Manager in Visual Studio
  • Update MySql.EntityFrameworkCore and MySql.Data to their latest stable builds
  • Rebuild and republish your app

2. Disable Single-File Publishing (Temporary Workaround)

If you can't upgrade packages right now, turn off single-file publishing:

  • In Visual Studio's publish profile settings, uncheck the "Single file" option
  • Or edit your .csproj file and remove/comment out this line:
    <PublishSingleFile>true</PublishSingleFile>
    

3. Verify Your Published Configuration

Double-check that your deployed appsettings.json matches your development setup:

  • Ensure the UsuarioConnection string has the correct server (if publishing to a remote machine, replace localhost with your MySQL server's IP/hostname), port, username, and password
  • Confirm appsettings.json exists in your publish output directory (set its properties to "Copy to Output Directory" if it's not being copied over)

4. Confirm MySQL Server Accessibility

Even with the firewall off, make sure:

  • The MySQL service is running on the target machine
  • You can connect using the same credentials from your connection string (test with MySQL Workbench or the mysql CLI)
  • If publishing remotely, MySQL is configured to accept external connections (check the bind-address setting in my.cnf/my.ini)

Bonus Troubleshooting Tip

To get more detailed EF Core logs for debugging, add this to your appsettings.json:

"Logging": {
  "LogLevel": {
    "Microsoft.EntityFrameworkCore": "Debug",
    "Microsoft.EntityFrameworkCore.Database.Connection": "Debug"
  }
}

This will show you granular details about how EF Core is attempting to connect, helping spot hidden configuration issues.

内容的提问来源于stack exchange,提问作者Paulo Machado

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 12:47:39