Base solution for your next web application
Open Closed

How do I change the connection string location? #3595


User avatar
0
strix20 created

We must use Azure Key Vault to store all secrets, including our connection strings.

I changed the CustomsEntityFrameworkCoreModule PreInitialize method to look like such:

public override void PreInitialize()
        {
            var connectionString = _appConfig["db-dev"];
            Configuration.DefaultNameOrConnectionString = connectionString;

            if (!SkipDbContextRegistration)
            {
                Configuration.Modules.AbpEfCore().AddDbContext<CustomsDbContext>(configuration =>
                {
                    CustomsDbContextConfigurer.Configure(configuration.DbContextOptions, connectionString);
                });
            }
        }

This works locally, but breaks in a hosted environment.

How can I replace the DefaultConnectionStringResolver?


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can use ReplaceService method in your module's PreInitialize method.

    Configuration.ReplaceService<IConnectionStringResolver, MyConnectionStringResolver>(DependencyLifeStyle..Transient);
    

    Thanks.

  • User Avatar
    0
    strix20 created

    Thanks for the response!

    I am curious, what I actually did was this:

    services.AddSingleton<IConnectionStringResolver, AzureKeyVaultConnectionStringResolver>();

    in my startup.cs.

    Is your method preferable? It seems your approach will override only for that module, but I don't know what modules embedded in ABP will be relying on a connection string?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @strix20,

    If a module calls ReplaceService in it's PreInitialize other than your module, the last one replaces the old ones. So, you should consider it with this information.

    Thanks.