I have an ASP.net MVC app connecting to (localdb)\v11.0 SQL Server database with a connection string using "Integrated Security" - works fine.
Now I want to connect to the same db with a User ID & PW.
I created the login & user: user100, gave it db_owner permission.
I can open the db with that ID/PW using SSMS.
But when I change my connection string to "User ID=user100 & Password=xxxx", I get the error:
CREATE DATABASE permission denied in database 'master'.\r\nCannot
attach the file
'C:\Projects\ExcellerWeb\ExcellerWeb\App_Data\ExcellerWebCopy.mdf'
as database 'ExcellerWebCopy'.
Why does it try to create a database that already exists?
And the following code returns TRUE:
System.Data.Entity.Database.Exists("UserConnection");
At first I thought it was EF Migrations, but it seems to happen on a simple test project without Migrations.
Here's my connection string:
<add name="UserConnection"
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\ExcellerWeb.mdf;Initial Catalog=ExcellerWeb;Integrated Security=False;User ID=user200; Password=****"
providerName="System.Data.SqlClient" />
Remove the Option AttachDbFilename from your connectionstring.
<add name="UserConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ExcellerWeb;Integrated Security=False;User ID=user200; Password=****"
providerName="System.Data.SqlClient" />
This option is there to attach the file when you open the connection.
For more details have a look at this or this MSDN-article
This isn't an answer to the original question, but we got the same error message when we moved a web app and database to new hardware, and a Google search led me here, so hopefully this might help someone...
In our case, the connection string that the web app used was fine, and the database already existed, so the error message was a bit of a surprise.
The problem turned out to be that the service account running the app pool in IIS didn't have the correct permissions on the database - when the account was added to the appropriate group, everything worked fine.
I created a new ASP.NET MVC 5 project in Visual Studio 2013 (Express for Web) and by default, the project uses LocalDb as its database, but how do you transfer or migrate the database to SQL Server?
I want to use SQL Server for the database instead of LocalDb. But how?
Notwithstanding this question is old, the answer didn't help me so I want to share how I solved it for my self.
On Server Explorer, find your ASPNet DB. Then open it using SQL Server Object Explorer.
Then go and hit Schema Compare option
Then on the the Schema Compare window for the Target database, select the SQL Server data base you want the ASPNet DB to integrate to. Then hit Compare button
Deselect all Delete actions for the target database, and leave selected all Add actions for the ASPNet DB, then hit Update button.
Finally, update your connection string so it points to your SQL Server DB
Got it!
Based on #warheat1990's answer, you just have to change the connection string. But #warheat1990's answer had a little too much change. So here's my original (LocalDb) connection string:
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-my_project-20150318100658.mdf;Initial Catalog=my_project-20150318100658;Integrated Security=True"
providerName="System.Data.SqlClient"/>
To connect it to SQL Server instead of LocalDB, I modified the connection string into:
<add name="DefaultConnection"
connectionString="Data Source=SERVERNAME\SQLEXPRESS;Initial Catalog=my_project;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Thanks to #warheat1990 for the idea of simply changing the Web.config. My first thoughts were to identify and use the feature that VS supplies, if theres any. Because Microsoft doesnt have a concise documentation on how to do this.
Change the connectionString in your web.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-KlikRX-20141203034323.mdf;Initial Catalog=aspnet-Test-20141203034323;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
to your own database connectionString, for example :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=7.7.7.7\sql;Initial Catalog=TestDB;User ID=sa;Password=sa" />
</connectionStrings>
It sounds like you may want to move the data from your local database to sql server. If so, the easiest way to do this would be to back up your local database and then restore it on the server.
To back up:
https://msdn.microsoft.com/en-us/library/ms187510.aspx#SSMSProcedure
To restore:
https://msdn.microsoft.com/en-us/library/ms177429.aspx
EDIT:
If you need to install an instance of SQL Server:
https://msdn.microsoft.com/en-us/library/ms143219.aspx
Overlord's migration example is spot on. My note at the end was a bit big for a comment, so here are the required changes to the web.config file. An old method on a local drive was to specify
Data Source=".\[InstanceName]
but may not work on newer interfaces, so replace [.\instance] with [ComputerName\instance] if you migrate forward. This is Visual Studio Pro 2017, SQL Server 2014 & Entity Framework 6.0.
1st update the connection string.. replace items in brackets with info needed to connect to your database.
<connectionStrings>
<add name="DefaultConnection"
connectionString="Initial Catalog=[DatabaseName];Integrated Security=True;User ID=[SQLASPNETUserName];Password=[UserPassword];"
providerName="System.Data.SqlClient" />
</connectionStrings>
next update the entity info.. The [InstanceName] used for SQL Server can be found from [SQL Server Mgmt] console - [Server Properties] - [Advanced] - [Filestream Share Name] & defaults as [MSSQLSERVER].
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="[ServerName]\[InstanceName]"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
for cloud or other multi-server database migrations, also review [sessionState] settings in web.config & replace [InProc] with [Custom]. [sessionState] comes between [/roleManager] & [/system.web]
this default for 1 db server
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider"
type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection"/>
</providers>
</sessionState>
& this replacement for mult-server or cloud environments
<sessionState mode="Custom" customProvider="DefaultSessionProvider">
I had the same problem and just solved this...so the main point is default connection string...which you need to modify correctly otherwise it is pointless..and impossible to connect properly. So copy all you aspnetroles...users table to online database( they should look the same as in your local database).
You can compare schema(local db) with real db. It is quit well explained by "Overlord" -> Explanation
But after lets now correctly modify defaultconnection string
That is my default string before modification:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-track_spa-20180502025513.mdf;Initial Catalog=aspnet-track_spa-20180502025513;Integrated Security=True" providerName="System.Data.SqlClient" />
That is my modified default string after modification:
<add name="DefaultConnection" connectionString="Data Source=servername,portnumber;Initial Catalog=AttendanceTrak;Integrated Security=False;User Id=****;Password=*****;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
servername - should be your server.
portnumber - should be your server port
It took me ages to finally get it working properly...but this small trick with default string just made it!
Hops this helps
In relation to OverLords answer, it worked perfectly for me thanks!
If anyone is struggling with the connection string use:
<add name="CONNECTIONSTRINGNAME" connectionString='data source= DATABASE SOURCE initial catalog="DATABASE NAME ";user id="USERID";password=PASSWORD;MultipleActiveResultSets=True;' providerName="System.Data.SqlClient" />
I had a similar problem, wanting to export from a local db to remote db-server and encountered an error message I couldn't find any information on, but the answer came to me when reading this post, so I'm submitting my answer here in case anyone else has the same problem.
I set up a solution with Individual User Accounts. VS conveniently creates a db (mdf-file under App_Data) and a connectionstring in the web.config.
In all my wisdom I thought: "Why not move this to a remote server?" So I did.
I restored the mdf file on the remote server, expanded it with some simple tables for my web site, created a new connection to the db and added a new ado.net edmx-file, removed the "DefaultConnection" in the web.config and updated the reference to my new connection in the ApplicationDBContext.
Pressed play, and... no sigar (when trying to log in).
The entity type IdentityUserLogin is not part of the model for the current context.
Turns out the IdentityDbContex prefers the "DefaultConnection" with the providerName="System.Data.SqlClient" so adding a new edmx-file with the providerName="System.Data.EntityClient" is no good.
Solution: As warheat1990 suggested, I updated (put back) the DefaultConnections and it's connectionstring value.
One might argue that I should have two seperate db's (one for users) and one for business stuff, but that's an other discussion.
This works for me..
Change the connection string in the web config file pointing to the database server, then run the application and register a user. Once registered successfully, go to SSMS and refresh the database and then the identity tables should appear.
Regards
I have my own box so I have complete control over the server. I'm using SQL server 2008. I run the application hoping it will create the database, but I keep getting the error:
Cannot open database "blah blah" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\appname'.
I've tried to go into SSMS and add the user IISAPPPOOL\appname but that hasn't helped any. Here's the connection string from my web.config
<add name="Context"
connectionString="Data Source=Server\DbInstance;Initial Catalog=blahblah;Integrated Security=True;User ID=myUser;Password=myPassword"
providerName="System.Data.SqlClient" />
and the initializer from my global.asax
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
Is the app failing because code first is failing and not creating the database or is it because of permissions issues? If I need to provide more specific information I can.
Well, one problem you have: you're defining both Integrated Security=True (i.e. Windows authentication) as well as User ID=...;Password=.... (i.e. SQL Server authentication) in your connection string.
Choose one or the other - but not both at the same time!
If you do have that user myUser specified as a login in your SQL Server, right now, it will not be used, because the Integrated Security=True setting will prevail ...
So change your connection string to:
<add name="Context"
connectionString="Data Source=Server\DbInstance;Initial Catalog=blahblah;User ID=myUser;Password=myPassword"
providerName="System.Data.SqlClient" />
and see if now, you're myUser user will be used.
i am really frustrated from this problem, i am trying to install DNN 7 in windows server 2008, i follwoed the steps in DNN site for installation, but when i run installer wizard i am getting connection error, i give the user permission on Folder level, as owner to the database, and still nothing, do i need to modify the connection in web.config file? is this correct
note:
database name: dnndev.me
user name: Administrator
Password: password
<add name="SiteSqlServer" connectionString="Data Source=.\SQLExpress;Integrated
Security=True;User Instance=True;AttachDBFilename=|DataDirectory|dnndev.me.mdf;"
providerName="System.Data.SqlClient" />
please help
this is my second post and i am not getting answer
How's your SqlServer set up? If the server is setup to use integrated security, you really don't need a username/password as Chris pointed out. In that case, the connection string in your web.config <ConnectionString> and <AppSettings> sections should look like
Data Source=.\SQLExpress;Integrated Security=True;User Instance=False;Database=dnndev.me;
Check out this blog for more details.
If you are using SQLExpress, and you have SQLExpress installed locally using the default instance, you don't need to worry about usernames/passwords for the database. DNN will attach the MDF file in the app_Data folder automatically.
If you are using another INSTANCE of SQL, than you will want to create the Database, and then associate a username/password that is DB_OWNER for that database, and provide that during the installation process. If that is the case, DO NOT choose the SQL Express option, use the SQL 2008 option.
So I'm moving my Asp.net mvc web app over to Arvixe shared hosting. This is the first time I've deployed an MVC app. I have been using SQL Server 2008 Express for the development database. Arvixe provides SQL Server 2008 or MySQL hosted databases.
A couple questions:
1.Can I use the mdf files from my Express database with the new Non-Express prodcution DB?
2.I'm having issues with my connection string. I changed the original web config connection string from this:
<add name="Database1ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
to this:
<add name="Database1ConnectionString" connectionString="Data Source=.;Integrated Security=SSPI;Initial Catalog=ProdsDB"
providerName="System.Data.SqlClient" />
Now I'm getting this error:
Cannot open database "ProdsDB" requested by the login. The login failed
I have setup the database called "ProdsDB" through the Arvixe control panel and added one user. Do I need to add the credentials somewhere in the connection string?
Yes. You're going to need to add User Id=myUsername;Password=myPassword; to the connection string, and you'll need to remove Integrated Security=SSPI
Of course, you'll need to set them to the username and password you created. Also, sometimes hosting providers host the database on a separate server. If that's the case, you'll have to specify the servername in place of the dot.