Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Azure connection keeps trying to open "master" despite connection string

I'm attempting to run the default "Welcome to ASP.NET MVC!" MVC3 application on Azure, using SQL Azure for the ASP.NET membership store, via ASP.NET Universal Providers. I have a SQL Azure database called "MyDatabase" with a new login and user both called "MyUser". The user is tied to the login and has been given the dbo_owner role for MyDatabase. MyDatabase is set as a linked resource to the web role in the management portal.

The site shows up fine on Azure, but trying any database operation, my database connection fails:

Cannot open database "master" requested by the login. The login failed. Login failed for user 'MyUser'.

Searching for answers, the usual response is to set the "Database" parameter in the connection string. I have indeed done this, but it still keeps trying "master." It's like it's being ignored:

Server=tcp:myserver.database.windows.net,1433;Database=MyDatabase;User ID=MyUser@myserver;Password=mypassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True

Other things I've ruled out:

  • I RDP'ed into my web role and confirmed that this connection string is showing up in my Web.config.
  • I tried "Initial Catalog" instead of "Database", but same results.
  • Possibly significant: SSMS connects to my database when I set "Connect to database:" to "MyDatabase", but fails with the same error if I leave that as default and put "Database=MyDatabase;" in Additional Connection Parameters. I'm not sure what that would mean?

Probably some newbie mistake here - what else should I check?

like image 551
user1454265 Avatar asked May 28 '26 17:05

user1454265


1 Answers

A look into the stack trace of the exception helped solve this one:

... at System.Data.SqlClient.SqlProviderServices.UsingMasterConnection(...) at System.Data.SqlClient.SqlProviderServices.DbDatabaseExists(...) at System.Data.Objects.ObjectContext.DatabaseExists() at System.Web.Providers.Entities.DatabaseCreationHelper.DatabaseExists(...) at System.Web.Providers.ModelHelper.CreateMembershipEntities(ConnectionStringSettings setting) at System.Web.Providers.DefaultMembershipProvider.Membership_CreateUser(...) ...

Since I was using a new database, the ASP.NET Universal Providers were trying to create the membership/role tables, and for some reason did actually need to use the master database to accomplish this. I added a new user to master, tied to my login, and made sure it had the "dbmanager" role so that it could create tables if needed.

I'm not 100% sure why it needs to go through master, but the stack trace makes it looks like it does so to check if the database you give it actually exists. (Please go ahead and comment if you know the definitive answer!)

like image 103
user1454265 Avatar answered May 30 '26 07:05

user1454265



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!