I remember how I used to work on this when doing backup / restore on servers. It basically boils down to how SQL Server works. There, SQL Server users and database users (SQL Server users who are database users are represented through mappings). However, they are not the same thing.
The SQL Server user belongs to SQL Server, the database user ONLY belongs to the corresponding database. What happens when you have a database user but not a SQL Server user? You cannot enter SQL Server explicitly.
Thus, after moving the database, I add the user that I need to log in, as for SQL Server users, using SSMS, delete the old database user (he received dependencies associated with him that prevent him from matching) and, finally make a new user in the database by matching my SQL user with the database with the appropriate permissions.
This approach is by no means elegant, but it works for 100% of the time without the need for code, and you should consider a more permanent system if you have automatic backup / restore. For this, I always did it.
RandomUs1r
source share