我正在使用this Microsoft.EnterpriseLibrary端口用于.Net Core。它需要一个配置文件app.config
,并在其中指定了连接字符串。我尝试使用与另一个工作项目中使用的连接字符串相同的字符串,但在这里不起作用。
如何为ASP.NET Core 2.1指定DB2连接字符串?
我这是我尝试过的:
<connectionStrings>
<add name="Development" connectionString="server=MY.SERVER.COM:446;database=DBXX;user id=USERXX;password=PASSWORDXX;" providerName="IBM.Data.DB2.Core"/>
</connectionStrings>
但是当我执行此操作时:
DatabaseFactory.SetDatabaseProviderFactory(
new DatabaseProviderFactory(
new SystemConfigurationSource(false).GetSection
),
false
);
var db = DatabaseFactory.CreateDatabase("Development");
它抛出了这个异常:
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Practices.EnterpriseLibrary.Data.dll but was not handled in user code: 'The connection string for the database 'Development' does not exist or does not have a valid provider.'
Inner exceptions found, see $exception in variables window for more details.
Innermost exception System.Configuration.ConfigurationErrorsException : The requested database Development does not have a valid ADO.NET provider name set in the connection string.
我认为提供商名称不正确,但是我在网络上找不到任何有效的示例。
答案 0 :(得分:1)
好,我解决了。原来,我找不到IBM.Data.DB2.Core的任何提供程序,因此我像这样配置了app.config
文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="system.data" type="System.Data.Common.DbProviderFactoriesConfigurationHandler, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="Development" />
<connectionStrings>
<add name="Development" connectionString="server=MY.SERVER.COM:446;database=DBXX;user id=USERXX;password=PASSWORDXX;" providerName="IBM.Data.DB2.Core"/>
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="IBM.Data.DB2.Core" />
<add name="DB2 Data Provider" invariant="IBM.Data.DB2.Core" description=".Net Framework Data Provider for DB2" type="IBM.Data.DB2.Core.DB2Factory, IBM.Data.DB2.Core" />
</DbProviderFactories>
</system.data>
</configuration>
在研究IBM程序集时,我发现有一个名为DbProviderFactory
的{{1}}实现类。如果DB2Factory
文件配置有app.config
部分(如上所示),则可以使用该类代替提供者名称。