Further, the readers would have heard of the destruction of the northern kingdom of Israel by the Assyrian armies late in the 8th century, decimating the people of Samaria, and seeding the land with Assyrian elements. That same army had come south to destroy Jerusalem and Judah, but had failed to do so for complex reasons right at the turn of the 8th-7th century B.C.E.
There is little reason for these Judeans of the 7th century to look kindly either at local foreigners or those whose more recent foreignness was more obvious by their dress and accents. All the more surprising, then, are the lines of Solomon's prayer.
When a ger who is not of your people Israel comes from a distant land because of (or "due to" or "on account of") your name—for they shall hear of your great name, your mighty hand and your outstretched arm (so the story of Exodus)—when a ger comes and prays toward (or "to") this house, then hear in heaven your dwelling place, and do all that the ger calls to you, so that all the peoples of the earth may know your name and fear (or "worship") you, as do your people Israel, and so that they may know that your name has been invoked on this house that I have built (1 Kgs. 8:41-43).
In other words, the brand new temple in Jerusalem is an open place of worship for all the people, particularly foreigners by whose prayers even "toward" the place will announce the universality of YHWH's covenant and love and will remind the Israelites that they are not the only ones who call upon the mighty name of YHWH.
All of us, like the dastardly David, need the amazing mercy of God if we are to be God's children in God's world. Equally, we need openness to those who are different if we are to experience fully that amazing mercy for ourselves. As you can see, even the tight straight jacket of a Deuteronomic ideology cannot, in the end, blunt the wonders of the biblical word for our own day.