Published on

The server principal is not able to access the database under the current security context in SQL Server

Authors

Problem

I found myself in a situation where SQL logins throw errors after restoring databases. It seems the mapping of logins and Dbs is broken.

Error:

The server principal is not able to access the database under the current security context in SQL Server

mappings can become disconnected after a restore or similar operation. In this case, the user may still exist in the database but is not actually mapped to a login. If that happens, We can run the following to restore the login:

Resolution:

USE {database};
ALTER USER {user} WITH login = {login}

Reference

Happy Coding :)