MVC3 connection string with Entity Framework - asp.net

As I have been following a MusicStore tutorial, new MVC3 project created a following connection string for me:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
My model tables got created in .\SQLEXPRESS and all the users/roles tables are stored in the aspnetdb.mdf in the App_Data folder.
I would like to have the model tables and user/roles tables on my .\SQLSERVER or localhost rather then scattered between .\SQLEXPRESS and the mdf file. I'm struggling with the right connection string. Can anyone suggest the right one please?
Everyone who does this has to make sure that name of the connection string should be the name of the context class derived from the *DbContext*

It sounds like you need to run aspnet_regsql on your ApplicationServices database. You can find it in C:\Windows\Microsoft.Net\Framework\4.xxx\aspnet_regsql.exe.
A default project uses ApplicationServices db as default for membership, roles, and profile. The sproc dbo.aspnet_CheckSchemaVersion is a sproc in the ASP.NET SQL Membership Provider schema.
Response to comment #1
Do you have a database instance running on your machine named SQLSERVER? The exception you mentioned is a common one, if asp.net cannot find the database using your connection string.
Response to comment #2
That makes sense. Integrated Security=true tells the connection to use the process running the MVC app (usually IIS app pool) for the sql server login. If you are testing from Visual Studio / Web Developer Express / etc, the web server is likely running on a process with your windows account. So if you can windows auth against the db server, MVC should also be able to.
User Instances are meant for SQLEXPRESS.

Related

SQL server database tables are not accessed

I have created a web-service using asp.NET with SQL Server 2005 database. Then I published it to local IIS, but when I publish it Visual Studio showed me an error stated that:
the process cannot access the file (path to database) because it is being used by another process.
so, I Detach the database. then I published the web-service. Now, when Browsing my web-service from IIS Manager, and try to test some methods, an error page appears to say:
System.Data.SqlClient.SqlException: Invalid object name 'Student'.
where Student is a table in my database.
my connectionString is:
<add name="sbms" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
Does Detaching the database cause this error? or there are errors in my ConnectionString? I search in Google and didn't find solution.
Please help me..
Detaching the database will cause the second error. The SqlClient can't find the table referenced by your code.
The first error comes from the database being opened in SQL Server in single user mode. Re-attach the database to SQL Server. Then change the connection settings to multiuser.
Detailed instructions for setting either single or multiple users

Creating an ASP .NET User Authentication Connection String using VMWare Fusion with OS Windows 7 on a MAC

I am attempting to create an ASP .NET User Authentication provider on a MAC using VMWare Fusion installed with Windows 7. I have created the SQLExpress datatbase named CustomerOrders, and have a connection to the DB in Server Explorer within Visual Studio 2010.
When I try to use the Web Site Administration tool I receive this error:
The connection name 'ApplicationServices' was not found in the applications configuration or the connection string is empty. (\vmware-host\shared folders\documents\visual studio 2010\Projects\CustomerOrders\CustomerOrders\web.config)
What I can’t figure out is the connection string I need to use in the Web.Config in order to setup the ASP .NET Authentication provider for this project running in a VMWare Fusion environment.
The connection string to my local database is:
Data Source=WIN-NOABML9MSVB\sqlexpress;Initial Catalog=CustomerOrders;Integrated Security=True
First, make sure you have the connection string specified in your web.config:
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=CustomerOrders;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
(Notice that I use .\SQLEXPRESS instead of the computer name, to make sure I use the local named instance)
Then, if you haven't done so already, make sure you create the CustomerOrders database and run the schema tool to create the database objects you need.

How to copy local Membership and Role database to remote MS SQL Database

I have made a MVC application that uses the built-in ASP.NET login functions. It works perfectly on my local machine. I have bought a webhosting service, because i want to publish my website to the internet.
How do i copy the membership and role database / tables to my new MS SQL Database and make it work with my current project?
In my Web.config i have:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
notice that
Instance=true
This means that SQL Server Express will open the aspnetdb.mdf file, creating it on the fly first if needed.
When i try to login on my website i get the following error:
Sorry, an error occurred while processing your request.
Thanks
You need to run aspnet_regsql located in C:\Windows\Microsoft.NET\Framework\v4.0.30319. Just point it to your remote sql server and it should take care of the rest.
You will also need to edit your connection string for application services to the one for your remote sql server.

How Can I upload ASPNETDB Database to Server?

I have uploaded my database backup and it works fine. but since I've created a new user from ASP.NET Configuration I've that it saves the user information in the ASPNETDB.
I already published my database from MVS and open it in M SQL Server Management Studio 2008 to create the backup but I haven't find the ASPNETDB in it.
my question is:
doesn't this database just upload it automatically when I upload my database? coz when I publish my website it doesn't appear.
if not, How can I upload ASPNETDB so I can login to my site because I'm using authorization to this user ?
Ok im gonna make a few assumptions here and explain them because this could be many things:
Assumption 1 (connection string issue). the user credentials for a database running on your local sql server are likely to be valid on the server on which you deploy to (seems to be the way of the world) try connecting to the remote database using the credentials in the remote "published" copy of the websites configuration file. (connection string)
Assumption 2 (publish settings not correct). when you use the "publish" option from within visual studio you only deploy the website to my knowledge but some say you can configure it to deploy the database as well.
I would suggest making a change (eg add a table to the aspnet database) on your dev machine then using publish to see if that new table appears on the remote server, if it doesn't go to the server explorer in visual studio and run the wizard by right clicking on your database and clicking on "publish to provider" i tend to push the entire db to a script file then manually run that on the remote database (seems the cleanest option).
Assumption 3 (does the database even exist on the server). I could be wrong here but from what you're saying it sounds like you published the application and not the database to the server ... that results in some nice yellow server errors.
Check that the database exists if not ... do as suggested in asumption 2 and push the database manually.
I suppose you're using the Membership API.
If you are using ASP.NET on a machine with SQL Server Express Edition, the required by Membership API underlying data store is created automatically for you when you create the first user. This functionality is provided through the SqlMembershipProvider. The SqlMembershipProvider automatically creates a new database in the website’s App_Data special directory called ASPNETDB.MDB. This database implements the complete schema, which is necessary for storing and managing user information, role information, user-role assignments etc.
You can easily check it. In your web.config you should see something like:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
So this database should have been uploaded along with your web site.
Please consider this tutorial if you want to create aspnetdb or necessary tables in your own database (which is a good praxis).
May be you will be also interested in the similar discussion here. Besides if you still want to get this .mdf database in SQL Server Management Studio you can learn how to attach a database here.

Failed to generate a user instance of SQL Server

I been trying to get my Visual Studio 2008 to run my Web Application on IIS7 on my Windows 7 machine but I just can't get it to work. I'm using the SQLEXPRESS I been following this Tutorial, but I get the following error message.
"Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed."
my connectionString seem to be ok
<add name="testConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|test.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
The site run with out database connection.
Any ideas and thoughts about this?
Thanks guys
Make sure the instance name is called "SQLEXPRESS" exactly.
Make sure the "test.mdf" file exists in the "App_Data" folder.
When you are adding the DB using Server Explorer, use "Test Connection" first.
In IIS7,
they use default asp.net database.
create a new application pool instead of using default.
and keep your database in that.
go to sql configuration manager and in the properties,select local system instead of build in system.
Now,it may work.

Resources