Multiple Database Connections

Hello,

In my app I have 2 Postgres SQL, which are configured in appSettings.json file like this

“ConnectionStrings”: {
“DefaultConnection”: “Server=0.1.1.6;User Id=abcuser;Password=abc123;Database=abcdb;Pooling=true;MinPoolSize=0;MaxPoolSize=200”,
“LogDbConnection”: “Server=0.1.1.6;User Id=abcuser;Password=abc123;Database=abcdbLog;Pooling=true;MinPoolSize=0;MaxPoolSize=200”

here is the Configure function

public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
             {
                 // Configure ASP.NET Core IOC Dependencies
             });
            builder.ConfigureServices((context, services) => {
                 services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory(
                     context.Configuration.GetConnectionString("DefaultConnection"), PostgreSqlDialect.Provider));
                 services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory(
                     context.Configuration.GetConnectionString("LogDbConnection"), PostgreSqlDialect.Provider));
             });            
            builder.ConfigureServices(services => services.AddSingleton<IAuthRepository>(c =>
                   new OrmLiteAuthRepository<AppUser, UserAuthDetails>(c.Resolve<IDbConnectionFactory>())
                   {
                       UseDistinctRoleTables = true
                   }));
            builder.ConfigureAppHost(appHost =>
            {
                var authRepo = appHost.Resolve<IAuthRepository>();
                authRepo.InitSchema();
                CreateUser(authRepo, "Admin", "admin@email.com", "Admin User", "abc123", roles: new[] { RoleNames.Admin });
            }, afterConfigure: appHost => appHost.AssertPlugin<AuthFeature>().AuthEvents.Add(new AppUserAuthEvents()));

            builder.ConfigureAppHost(appHost =>
            {
                var appSettings = appHost.AppSettings;
                appHost.Plugins.Add(new AuthFeature(() => new CustomUserSession(),
                    new IAuthProvider[] {
                        new BasicAuthProvider(appSettings),     /* Sign In with Username / Password credentials */
                        new JwtAuthProvider(appSettings)
                        {
                            AuthKey = AesUtils.CreateKey(),
                            UseTokenCookie = false,
                            RequireSecureConnection = false,
                            ExpireTokensIn = TimeSpan.FromSeconds(60),
                            ExpireRefreshTokensIn = TimeSpan.FromSeconds(120),

                        }
                        //new FacebookAuthProvider(appSettings),        /* Create App https://developers.facebook.com/apps */
                        //new GoogleAuthProvider(appSettings),          /* Create App https://console.developers.google.com/apis/credentials */
                        //new MicrosoftGraphAuthProvider(appSettings),  /* Create App https://apps.dev.microsoft.com */
                    }));

                appHost.Plugins.Add(new RegistrationFeature()); //Enable /register Service

                //override the default registration validation with your own custom implementation
                //appHost.RegisterAs<CustomRegistrationValidator, IValidator<Register>>();
                appHost.RegisterAs<CustomRegValidator, IValidator<Register>>();
            });
        }

  public void CreateUser(IAuthRepository authRepo, string userName, string email, string name, string password, string[] roles)
        {
            if (authRepo.GetUserAuthByUserName(email) == null)
            {
                var newAdmin = new AppUser {UserName=userName, Email = email, DisplayName = name };
                var user = authRepo.CreateUserAuth(newAdmin, password);
                authRepo.AssignRoles(user, roles);
            }
        }

May main database is abcdb I want to do everything in the abcdb and when I need to do any logging I want to keep that logging in the different database abcdbLog. But currently when I am executing the above code, seems its trying to do everything in the abcdbLog database.

Can you kindly guide me to the right direction how can I select specific database for specific functionality.

Thanks

There should only be a singleton instance of OrmLiteConnectionFactory registered with the default connection, every other connection needs to be registered as a named connection. You can find examples in the docs:

Thanks @mythz, I will go through the documentation you have referenced and will try to move on.