C# 使用.NET Core 2.1配置企业库
我正在将解决方案从.NET framework 4.7.1迁移到.NET Core 2.1,并且在企业库6(使用EnterpriseLibrary.Data.NetCore Nuget软件包)方面遇到问题 我正试图从连接字符串创建一个数据库,并将其注册到Autofac 原始代码如下所示:C# 使用.NET Core 2.1配置企业库,c#,asp.net-core,migration,asp.net-core-mvc,enterprise-library,C#,Asp.net Core,Migration,Asp.net Core Mvc,Enterprise Library,我正在将解决方案从.NET framework 4.7.1迁移到.NET Core 2.1,并且在企业库6(使用EnterpriseLibrary.Data.NetCore Nuget软件包)方面遇到问题 我正试图从连接字符串创建一个数据库,并将其注册到Autofac 原始代码如下所示: builder.Register(c => new DatabaseProviderFactory().Create(DefaultConnection) as SqlDatabase).Instance
builder.Register(c => new DatabaseProviderFactory().Create(DefaultConnection) as SqlDatabase).InstancePerLifetimeScope();
其中,DefaultConnection
是一个字符串常量,表示web.config
连接字符串
在web.config
中,我有:
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="DefaultConnection" />
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="*****************" />
</connectionStrings>
但这会引发以下异常:
The connection string for the database 'DefaultConnection' does not exist or does not have a valid provider.
- The requested database DefaultConnection is not defined in configuration
我有一个web.config
,在解决方案中具有相同的设置,但它没有被读取,我还有一个带有连接字符串的appsettings.json
我使用appsettings.json尝试了以下代码:
builder.Register(_ => new SqlDatabase(configuration.GetConnectionString("DefaultConnection"))).InstancePerLifetimeScope();
这似乎是可行的,但当我调用Microsoft.Practices.EnterpriseLibrary.Data.DatabaseExtensions.ExecuteSprocAccessor(…)
我有个例外
Parameter discovery is not supported for connections using GenericDatabase.
You must specify the parameters explicitly, or configure the connection to use a type deriving from Database that supports parameter discovery.'
有没有人知道正确的方法是什么 请展示“一切”。Aka,显示您的appsettings.json和web.config。
Parameter discovery is not supported for connections using GenericDatabase.
You must specify the parameters explicitly, or configure the connection to use a type deriving from Database that supports parameter discovery.'