Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

When I try to install a new database, I'm getting this error from SQL Server 2008.

The server principal "<mydbuser>" is not able to access the database "<mydatabase>" under the current security context.

I looked it up and it says it has something to do with orphaned users when doing a restore. This connection has nothing to do with a restore, it's a brand new database, user and connection, so why am I getting this problem?

Also the process I'm using to create the database and user/login, is the same for all of my databases, so why does this occur sometimes?

Any ideas?

Thanks.

EDIT: per request here's some code that fails, but understand that this works with every other application I've built. Also this is built into an ASP.NET MVC application.

System.Web.Management.SqlServices.Install(
    config.Database.DataSource.Trim(), 
    config.Database.UserName, 
    config.Database.Password, 
    config.Database.InitialCatalog, 
    System.Web.Management.SqlFeatures.All);


if (!Roles.RoleExists(role.Name))
    Roles.CreateRole(role.Name);  <-- this fails

Oh, another thing that doesn't make sense is that the database in the error message is not the database I'm trying to install to. It is one of the databases in this SQL Server instance (along with a bunch of others) but it's completely different. I can't figure out where it's getting this.

Example: the database I'm trying to install to is 'Rapper' and the database shown in the error message is 'GPS'. Why?

share|improve this question
2  
Maybe you could show the code you're using to "install a new database"? –  Aaron Bertrand Jul 11 '12 at 17:37

1 Answer 1

I can't create any comment yet. I don't have the required reputation. So I will post an answer!!!

You can view orphanage users in a database using:

exec sp_change_users_login 'report'

And it also might be because the default database of you login try:

exec sp_helplogins 'Login1'

You can change the default db with:

ALTER LOGIN Login1 WITH DEFAULT_DATABASE=test1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.