ServiceStack 使用自定义 app.config 设置在 IDE 中运行迁移

问题描述 投票:0回答:1

我按照此处提供的指导使用显式测试类在我的 IDE 中执行迁移。

但是,

ResolveDbFactory
失败,并出现有关连接字符串为空的错误。

static IDbConnectionFactory ResolveDbFactory() => new ConfigureDb().ConfigureAndResolve<IDbConnectionFactory>();

我的主项目中的

ConfigureDb
类的配置如下:

public class ConfigureDb : IHostingStartup
{
    public void Configure(IWebHostBuilder builder) => builder
        .ConfigureServices((context, services) => {
            services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory(
                context.Configuration.GetConnectionString("DefaultConnection"),
                SqlServer2019Dialect.Provider));
        });
}

其中

DefaultConnection
来自
AppHost
中配置的 appsettings.secrets.json 文件,如下所示: 公共类AppHost:AppHostBase,IHostingStartup

{
    public void Configure(IWebHostBuilder builder) => builder
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddJsonFile("appsettings.secrets.json",
            optional: true,
            reloadOnChange: true);
        })
        .ConfigureServices(services =>
        {
            // Configure ASP.NET Core IOC Dependencies
            services.AddScoped<IEmailFactory, EmailFactory>();
            services.AddScoped<IEmailSender, SendGridEMailSender>();
        });

为什么运行

static IDbConnectionFactory ResolveDbFactory() => new ConfigureDb().ConfigureAndResolve<IDbConnectionFactory>();
行没有解析连接字符串?

servicestack ormlite-servicestack
1个回答
0
投票

在测试中

appsettings.json
,您可以指定包含应用程序主机文件夹
appsettings.json
的主机项目相对于运行测试的
/bin/Debug/net8.0
,例如:

我的应用程序.测试

{
    "HostDir": "../../../../MyApp"
}
© www.soinside.com 2019 - 2024. All rights reserved.