ServiceStack Running Migrations in IDE with custom app.config settings

23 Views Asked by At

I'm following the guidance provided here to execute migrations within my IDE using an explicit Test Class.

However, the ResolveDbFactory fails with an error about the connection string being empty.

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

The ConfigureDb class in my main project is configured like so:

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

Where DefaultConnection comes from a appsettings.secrets.json file configured in the AppHost like so: public class 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>();
        });

Why is it that the running of the static IDbConnectionFactory ResolveDbFactory() => new ConfigureDb().ConfigureAndResolve<IDbConnectionFactory>(); line is not resolving the connection string?

1

There are 1 best solutions below

0
mythz On

In your Tests appsettings.json you can specify where your Host project containing your App's Host folder appsettings.json is relative to /bin/Debug/net8.0 that the tests are run from, e.g:

MyApp.Tests

{
    "HostDir": "../../../../MyApp"
}