错误:无法加载文件或程序集'System.Configuration.ConfigurationManager',找不到指定的文件。

32

我正在尝试使用ADO.NET从.NET Core 3.1连接到Oracle数据库,以下是我的代码:

private OracleConnection GetOracleConnection()
{
            string conString = "Data Source=Q550.adr.XXXX.com;Persist Security,Info=True;User ID=XXXX;Password=CCC39";
            OracleConnection con = new OracleConnection(conString);
            return con;
}

但是当这个函数执行时,在第2行发生了以下异常

System.TypeInitializationException:'OracleInternal.Common.ProviderConfig'的类型初始值设定项引发了异常

FileNotFoundException:无法加载文件或程序集“System.Configuration.ConfigurationManager,Version=4.0.2.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”。系统找不到指定的文件。 **

有人可以告诉我发生了什么?


尝试这个: 转到项目属性,取消选中偏好32位。 - Clint
2
你是否指的是 ConfigurationManager.ConnectionStrings 或者在你的命名空间中添加了 ConfigurationManager?在 .net core 中,你需要将 System.Configuration 包含为一个包才能使用 ConfigurationManager 对象来访问 AppSettings 和 ConnectionStrings。 - Ak777
1
或者,您可以从NuGet安装“System.Configuration.ConfigurationManager”包。 - Clint
“prefer 32 bit”未被选中(在创建项目时默认情况下),并不能解决问题。按照您建议的安装System.Configuration.ConfigurationManager可以解决问题。 - Sebastián Palma
尝试连接MySQL时,我也遇到了完全相同的问题。 - Leo Muller
1
安装软件包并不能解决部署问题,因为dll文件没有被包含在构建中。 - B2K
2个回答

45

对于MySQL,我需要这个,还有System.Management,不知道为什么。 - Leo Muller

0

我尝试添加对NuGet包System.Configuration.ConfigurationManager的引用,但是该库的最新版本(7.0)不再包含ConfigurationManager。因此,它仍无法编译。经过大量搜索,我发现我必须包括NuGet包的4.5版本。


我刚刚使用 dotPeek 进行了双重检查,确认 .NETCoreApp v.6.0 的 dll 版本 7.0.0.0 确实包含 ConfigurationManager 类。 - XtianGIS
我试了7.0,不好用,降级到4.5,成功了!谢谢! - alexbrina

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接