无法打开NpgsqlConnection:System.Numerics.Vectors程序集加载失败求助
Npgsql连接PostgreSQL时System.Numerics.Vectors依赖问题
我正在开发一款C#应用,使用Npgsql从PostgreSQL数据库获取数据,但尝试打开数据库连接时遇到错误。
使用的代码
public static DataTable FetchMasterData() { NpgsqlConnection npgsqlConn = new NpgsqlConnection(Connection.ConnectionStringPostgreSQL); DataTable classMasterTbl = new DataTable(); string sqlQuery = string.Empty; NpgsqlCommand npgsqlCommand = null; try { sqlQuery = @"SELECT * FROM ClassMaster"; npgsqlCommand = new NpgsqlCommand(sqlQuery, npgsqlConn); npgsqlConn.Open(); // Getting Error here NpgsqlDataAdapter dataAdapter = new NpgsqlDataAdapter(npgsqlCommand); dataAdapter.Fill(classMasterTbl); return classMasterTbl; } catch (Exception dalExce) { // Handle exception } finally { npgsqlCommand?.Dispose(); npgsqlConn.Close(); } }
错误信息
尝试打开连接时,收到如下错误:
Could not load file or assembly 'System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
相关配置文件
App.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
Packages.config
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net461" /> </packages>
.csproj引用
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath> </Reference>
环境信息
- Npgsql版本:
4.1.14.0 - .NET版本:
4.6.1 - 已尝试操作:清理并重新生成解决方案
问题
- 为什么System.Numerics.Vectors程序集无法被找到?
- 如何解决这个依赖问题?
解决方案
问题原因
- 版本不匹配:Npgsql 4.1.14.0依赖的是System.Numerics.Vectors 4.1.3.0,但当前安装的是4.5.0版本,虽配置了绑定重定向,但规则可能未覆盖请求版本或未生效。
- 引用路径与输出问题:.csproj中HintPath指向net46目录的dll,而项目目标框架是net461;且可能该dll未被复制到应用输出目录,导致运行时找不到。
- 绑定重定向未生效:手动配置的重定向规则可能未被正确识别,或NuGet包缓存导致依赖版本混乱。
修复步骤
调整绑定重定向规则
确保App.config中的重定向规则覆盖Npgsql请求的4.1.3.0版本,或开启自动生成重定向:<dependentAssembly> <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" /> </dependentAssembly>也可右键项目→属性→应用程序→勾选
自动生成绑定重定向,让系统自动生成适配的规则。确保程序集复制到输出目录
右键项目中的System.Numerics.Vectors引用→属性,将复制本地设为True;检查输出目录(如bin/Debug)是否存在该dll,若缺失手动从packages/System.Numerics.Vectors.4.5.0/lib/net461(或net46)目录复制。对齐依赖版本
- 降级System.Numerics.Vectors到Npgsql依赖的4.1.3.0版本:
Install-Package System.Numerics.Vectors -Version 4.1.3.0 - 或升级Npgsql到更高版本(如5.x/6.x),新版本对System.Numerics.Vectors的依赖更兼容4.5.0版本(注意升级后需适配新API)。
- 降级System.Numerics.Vectors到Npgsql依赖的4.1.3.0版本:
清理NuGet缓存并还原
执行命令清理缓存:nuget locals all -clear右键解决方案→还原NuGet包,确保所有依赖正确下载引用。
内容的提问来源于stack exchange,提问作者PPB
相关产品推荐
相关产品推荐

