.NET Core中使用MySQL.Data出现程序集加载失败问题求助
Hey there! As a fellow RAGE:MP developer who’s been in your shoes, let’s break down how to fix this assembly loading issue you’re facing when trying to connect to MySQL with Oracle’s MySQL.Data library.
Why This Happens
Oracle’s MySQL.Data library has a dependency on System.Drawing.Common in certain versions, and RAGE:MP’s server runtime might not include this assembly by default—especially if your project’s target framework doesn’t match what the server expects. The error you’re seeing means the server can’t locate the exact version of System.Drawing.Common that MySQL.Data is requesting.
Step-by-Step Solutions
1. Switch to MySqlConnector (Recommended)
Oracle’s MySQL.Data is often bloated and carries unnecessary dependencies like System.Drawing.Common. A far better alternative is MySqlConnector, a lightweight, open-source MySQL client that avoids this exact issue. Here’s how to make the switch:
- Uninstall the MySQL.Data NuGet package from your project.
- Install the
MySqlConnectorNuGet package instead. - Update your connection code to use the MySqlConnector implementation:
using MySqlConnector; // ... string SQLConnection = $"SERVER={sql.Host};PASSWORD={sql.Password};UID={sql.Username};DATABASE={sql.Database}"; conn = new MySqlConnection(SQLConnection); try { conn.Open(); NAPI.Util.ConsoleOutput("MySQL connection is up!"); IsConnectionSetUp = true; } catch(Exception ex) { NAPI.Util.ConsoleOutput(ex.ToString()); }
This is the most reliable fix for RAGE:MP environments, as MySqlConnector is widely used in the community and cuts out extra, unneeded dependencies.
2. Match System.Drawing.Common Version to MySQL.Data’s Requirement
If you want to stick with Oracle’s MySQL.Data:
- Note the exact version of
System.Drawing.Commonyour error mentions (in your case,4.0.2.0). - Install that specific version via NuGet:
Install-Package System.Drawing.Common -Version 4.0.2.0 - Locate the
System.Drawing.Common.dllfile from your project’s NuGet packages folder (usually underpackages/System.Drawing.Common.4.0.2.0/lib/net4xx/—pick the folder matching your project’s target framework). - Copy this DLL into your RAGE:MP server’s
server-filesdirectory, or directly into the folder where your compiled script DLL is stored.
3. Align Your Project’s Target Framework with RAGE:MP’s Runtime
RAGE:MP servers typically run on .NET Framework 4.7.2 (double-check your server’s docs to confirm). If your project is targeting .NET Core/.NET 5+, this can cause assembly mismatches:
- In Visual Studio Code, open your project’s
.csprojfile. - Set the
TargetFrameworkto match the server’s runtime, for example:<TargetFramework>net472</TargetFramework> - Recompile your script and redeploy it to the server.
4. Fix Missing Dependencies Like System.Security.Permissions
Your server console mentioned missing System.Security.Permissions too—here’s how to fix that:
- Install the
System.Security.PermissionsNuGet package that matches your target framework. - Copy the corresponding DLL from the NuGet packages folder into your server’s runtime directory, just like you did with
System.Drawing.Common.
Final Notes
Always clean and rebuild your project after making NuGet or framework changes, and double-check that all required DLLs are present in the server directory where your script runs.
内容的提问来源于stack exchange,提问作者Kopra

