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.EntityFrameworkCoreandMySql.Datato 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
.csprojfile 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
UsuarioConnectionstring has the correct server (if publishing to a remote machine, replacelocalhostwith your MySQL server's IP/hostname), port, username, and password - Confirm
appsettings.jsonexists 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
mysqlCLI) - If publishing remotely, MySQL is configured to accept external connections (check the
bind-addresssetting inmy.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

