问题
I am using Visual Studio Code, and I'm trying to connect to my MySql server (5.5). I have downloaded the latest (6.10.6) MySql connector and installed it. I had to research to add references in VS Code, but I managed it. Now my .csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="Lib\MySql.Data.dll">
<HintPath>MySql.Data</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
</ItemGroup>
</Project>
I have copied the content of C:\Program Files (x86)\MySQL\MySQL Connector Net 6.10.6\Assemblies\v4.5.2 to myProject\Lib. At compile time there are no errors, all green. But when I try to connect using this code
using System.Data;
using System.Security.Permissions;
using MySql.Data;
using MySql.Data.MySqlClient;
public static bool Connect(){
string str = "server=192.168.0.160;user=usr;database=DB;port=3306;password=pass";
MySqlConnection connection = new MySqlConnection(str);
connection.Open();
System.Console.WriteLine(connection.State);
return true;
}
it fails, and produces this error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in MySql.Data.dll: 'Could not load file or assembly 'System.Security.Permissions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system can't find the file.'
I do not know why, as the using directive is not signaling an error.
回答1:
The assembly System.Security.Permissions
is currently not available for .NET core applications so my guess is you are using an older version of MySQL Database Provider that is not compatible with .NET core 2.
According the official documentation .NET core 2.0 is only supported from version 6.10.
Try installing the latest version from: https://dev.mysql.com/downloads/connector/net/6.10.html
Edit
If you already have that version and it is still not working, might be that you are missing some references. Why don't you try using the official NuGet instead of referencing the dll in the GAC, here is the command:
Install-Package MySql.Data -Version 6.10.6
If you are using VS Code, you can use the NuGet package manager extension to manage the packages directly from the editor: https://marketplace.visualstudio.com/items?itemName=jmrog.vscode-nuget-package-manager
Edit 2
Seems it might be a bug as I found this question .NET Core 2 with MySql.Data results in permission error and the accepted answer recommends updating to version 8.
So try to update to version 8.0.10-rc and let the problem be gone, here is the NuGet command:
Install-Package MySql.Data -Version 8.0.10-rc
来源:https://stackoverflow.com/questions/48602743/io-filenotfoundexception-in-mysql-data-dll-cant-load-system-security-permissio