City attachment captures individual migrants' emotional bonds to their host city and is an essential indicator of their social integration. Although a growing literature has investigated the impact of the residence-based social environment on city attachment, few works have attempted to unravel the effects of social exposure in an individual's daily activity space. Drawing on an activity diary survey and census data from Shanghai, we examine how social exposure in residential areas and activity spaces influences an individual's city attachment. Specifically, we compare these differences among local residents, urban migrants, and rural migrants. Social exposure is measured by time-weighted migrant exposure in residential and activity spaces, capturing the social mix among locals and migrants. We find that the city attachment of local residents is greater than that of migrants. There are also significant differences in migrant exposure among the three groups: Local residents have the lowest migrant exposure, while rural migrants possess the highest.Moreover, people living in residential spaces with higher migrant exposure experience lower exposure levels in activity spaces. Each of the three groups also reveals a distinct impact of social exposure on city attachment. For local residents, residence-based migrant exposure has a significant positive impact on their city attachment. For urban migrants, however, the effect of migrant exposure on their city attachment is statistically insignificant. Finally, for rural migrants, activity-based migrant exposure negatively impacts their city attachment. Accordingly, this study indicates that the social mix in both residential areas and activity spaces, for example, workplace or routine activity locations, should be evaluated to promote individuals' social integration.