Ben*_*eld 5 c# migration enterprise-library asp.net-core-mvc asp.net-core
我正在将解决方案从 .NET Framework 4.7.1 迁移到 .NET Core 2.1,并且遇到了 Enterprise Library 6 的问题(使用 EnterpriseLibrary.Data.NetCore Nuget Package)
我正在尝试从连接字符串创建数据库并将其注册到 Autofac。
原始代码如下所示:
builder.Register(c => new DatabaseProviderFactory().Create(DefaultConnection) as SqlDatabase).InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)
其中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>
Run Code Online (Sandbox Code Playgroud)
这很好用。
在我的 .NET Core 2.1 解决方案中,我使用以下代码
builder.Register(_ => new DatabaseProviderFactory().Create(Constants.DefaultConnection) as SqlDatabase).InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)
但这会引发以下异常:
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
Run Code Online (Sandbox Code Playgroud)
我web.config在解决方案中有一个具有相同设置的文件,但它没有被读取,我也有一个appsettings.json带有连接字符串的文件。
我已经尝试使用以下代码appsettings.json:
builder.Register(_ => new SqlDatabase(configuration.GetConnectionString("DefaultConnection"))).InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但是当我打电话时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.'
Run Code Online (Sandbox Code Playgroud)
有谁知道执行此操作的正确方法是什么?