If you download ASP.NET MVC from Microsoft, install it and create a MVC website you might run into a couple of gotchas in regards to the database. First of all ASP.NET MVC use SQL Server Express by default in the connection string. So if you were to run your ASP.NET MVC website, then clicking on login you would get this error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)

A little more info below that error will tell you that it is trying to create a SQL Server Express database. Here is that info

SQLExpress database file auto-creation error:

The connection string specifies a local Sql Server Express instance using a database location within the applications App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

If the applications App_Data directory does not already exist, the web server account must have read and write access to the applications directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.

If the applications App_Data directory already exists, the web server account only requires read and write access to the applications App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the applications App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server accounts credentials are used when creating the new database.

Sql Server Express must be installed on the machine.
The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.

So I don’t have the Express version of SQL Server on my machine. The first thing I have to do is open the Web.config file and change the connection string from this

XML
1
2
3
4
5
<connectionStrings>
        <add name="ApplicationServices" 
            connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
        User Instance=true" providerName="System.Data.SqlClient"/>
    </connectionStrings>
<connectionStrings>
		<add name="ApplicationServices" 
			connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
		User Instance=true" providerName="System.Data.SqlClient"/>
	</connectionStrings>

to this

XML
1
2
3
4
<connectionStrings>
        <add name="ApplicationServices" 
            connectionString="data source=.;Integrated Security=SSPI;Initial Catalog=aspnetdb"/>
    </connectionStrings>
<connectionStrings>
		<add name="ApplicationServices" 
			connectionString="data source=.;Integrated Security=SSPI;Initial Catalog=aspnetdb"/>
	</connectionStrings>

Now let’s try again by clicking on the login page. Still a problem for me, now I get the following error

Cannot open database “aspnetdb” requested by the login. The login failed.
Login failed for user ‘Denis-PCDenis’.

Okay this makes sense since I don’t have this database, I need to create it. There are 2 ways you can do this. Either navigate to C:WindowsMicrosoft.NETFrameworkv2.0.50727 and execute aspnet_regsql.exe or run the Visual Studio command prompt.

Start –> All Programs –> Microsoft Visual Studio 2008 –> Visual Studio Tools –> Visual Studio 2008 Command Prompt

After the Visual Studio Command Prompt is up type aspnet_regsq and hit enter

Below is a screenshot of what it looks like if you run it from the command prompt

Click Next on the first screen of the wizard, this will bring you to the screen shown below

Leave the top option selected and click Next, this will bring you to the screen shown below

In the Server field type your SQL Server name or type a dot if it is on your local server, type a username and password if you use sql authentication or select windows authentication if you use that. Leave the last option as it is and click Next. Now you will see a summary screen, click Next on the summary screen and then if everything went as it should you should see a Database has been created or modified screen, click finish to exit the wizard.

Now you can go back to your ASP.NET MVC site, hit F5 in Visual Studio and click Log On, click on the register link, fill out the fields and you should see something like the screen below.

Congratulations, you have now completed a small step toward you first ASP.NET MVC site, you now have a functional site but you need to add Models, Controllers and Views.