internal DbConnection GetTenantConnection()
        {
            if (_tenantConnection != null)
            {
                return(_tenantConnection);
            }

            var cnn = GetMasterConnection();

            using var cmd = cnn.CreateCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_GetClientRepositoryDetails";
            cmd.AddParameter("@paramDomain", DbType.String, _subDomain);

            var row = cmd.GetRows()?.SingleOrDefault();

            if (row == null)
            {
                throw new InvalidOperationException("Tenant information not configured");
            }

            var credentials = new DatabaseCredentials(_dbProviderFactory)
            {
                Database = row.Database,
                UserName = this.UserDbUsername ?? row.UserName,
                Password = this.UserDbPassword ?? row.Password,
                Server   = row.Servername,
                Schema   = row.Schema
            };

            _tenantConnection = _dbProviderFactory.CreateConnection();

            Debug.Assert(_tenantConnection != null, nameof(_tenantConnection) + " cannot be null");
            _tenantConnection.ConnectionString = credentials.BuildConnectionString();
            _tenantConnection.Open();

            return(_tenantConnection);
        }
        internal DbConnection GetMasterConnection()
        {
            if (_masterConnection != null)
            {
                return(_masterConnection);
            }

            var credentials = new DatabaseCredentials(_dbProviderFactory)
            {
                Database = _configuration[SettingKeys.RepositoryCatalog],
                UserName = _configuration[SettingKeys.RepositoryUserName],
                Password = _configuration[SettingKeys.RepositoryPassword],
                Server   = _configuration[SettingKeys.RepositoryServer]
            };

            Debug.Assert(_dbProviderFactory != null, nameof(_dbProviderFactory) + " cannot be null");
            _masterConnection = _dbProviderFactory.CreateConnection();

            Debug.Assert(_masterConnection != null, nameof(_masterConnection) + " cannot be null");
            _masterConnection.ConnectionString = credentials.BuildConnectionString();
            _masterConnection.Open();

            return(_masterConnection);
        }