我正在使用企业库DAAB 6.要与Oracle数据库通信,我使用的是EntLib Contrib库。
我的应用程序的配置文件看起来像
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="OracleConnectionString">
<providerMappings>
<add databaseType="EntLibContrib.Data.OdpNet.OracleDatabase, EntLibContrib.Data.OdpNet, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Oracle.DataAccess.Client" />
</providerMappings>
</dataConfiguration>
<connectionStrings>
<add name="OracleConnectionString" connectionString="Data Source=localhost:1521/dev;User ID=db_owner;Password=admin;"
providerName="Oracle.DataAccess.Client" />
</connectionStrings>
<configuration>
我已经使用NuGet安装了所有必需的包。 Package.Config文件如下所示:
<packages>
<package id="EnterpriseLibrary.Common" version="6.0.1304.0" targetFramework="net45" />
<package id="EnterpriseLibrary.Data" version="6.0.1304.0" targetFramework="net45" />
<package id="EntLibContrib.Data.OdpNet" version="6.0.1304.0" targetFramework="net45" />
</packages>
我还添加了对 Oracle.DataAccess.dll 文件的引用。
我使用以下代码行连接到数据库
var database = new DatabaseProviderFactory().CreateDefault();
上面的行会生成一个非常奇怪的错误消息。完整的堆栈跟踪如下:
System.InvalidOperationException: The connection string for the default database 'OracleConnectionString' does not exist or does not have a valid provider.
---> System.Configuration.ConfigurationErrorsException: The requested database OracleConnectionString does not have a valid ADO.NET provider name set in the connection string. (C:\Test\OraConsole\bin\Debug\OraConsole.vshost.exe.Config line 13)
at Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSyntheticConfigSettings.GetDatabase(String name)
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory.DatabaseConfigurationBuilder.<CreateDefault>b__2(String n)
--- End of inner exception stack trace ---
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory.DatabaseConfigurationBuilder.<CreateDefault>b__2(String n)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory.DatabaseConfigurationBuilder.CreateDefault()
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseProviderFactory.CreateDefault()
at Data.OdpNet.QuickStarts.Console.Program.Main(String[] args) in c:\Test\OraConsole\Program.cs:line 15
tnsping 适用于Oracle数据库。
我已用Google搜索此特定错误消息,但我无法找到此问题的解决方案。我还尝试了Contrib源提供的QuickStart代码示例。它也会生成相同的错误消息。
答案 0 :(得分:1)
听起来DbProviderFactories没有为Oracle设置。通常在安装ODP.NET时,机器配置将更新为:
<system.data>
<DbProviderFactories>
<add name="Oracle Data Provider for .NET Version 11.1.0.6.0"
invariant="Oracle.DataAccess.Client Version 11.1.0.6.0"
description="Oracle Data Provider for .NET Version 11.1.0.6.0"
type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess,
Version=2.111.6.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>
见ODP.NET Configuration。我会验证machine.config是否已设置。您还可以在app / web.config中设置provderfactories。