Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

c# - IO.FileNotFoundException in MySql.Data.dll: Can't load System.Security.Permissions

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="LibMySql.Data.dll">
      <HintPath>MySql.Data</HintPath>
      <SpecificVersion>False</SpecificVersion> 
    </Reference>
  </ItemGroup>
</Project>  

I have copied the content of C:Program Files (x86)MySQLMySQL Connector Net 6.10.6Assembliesv4.5.2 to myProjectLib. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...