This is the ASP.NET MVC 4 version of the MVC Music Store sample application.
- Visual Studio 2022 (or Visual Studio 2012 or later)
- SQL Server LocalDB - Installed with Visual Studio 2022
Note: SQL Server LocalDB is automatically installed with Visual Studio 2022 as part of the "ASP.NET and web development" workload or the "Data storage and processing" workload.
The MVC Music Store application uses SQL Server LocalDB with database files located in the App_Data folder:
MvcMusicStore.mdf- Main application databaseaspnet-MvcMusicStore-20120831200627.mdf- SimpleMembership database for user authentication
The application is configured to automatically create and seed the database on first run:
- Open the solution in Visual Studio:
MvcMusicStore.sln - Build the solution (Ctrl+Shift+B)
- Run the application (F5)
The database will be automatically:
- Attached to LocalDB
- Initialized with the schema
- Seeded with sample music store data (albums, genres, artists)
This is handled by the SampleData class in Models/SampleData.cs.
If you need to manually connect to the database using Server Explorer in Visual Studio:
-
Open Server Explorer
- Go to
View>Server Explorer(or pressCtrl+Alt+S)
- Go to
-
Add a Data Connection
- Right-click
Data Connections>Add Connection…
- Right-click
-
Configure the Connection
- Data Source: Select
Microsoft SQL Server (SqlClient) - Server Name: Enter
(LocalDB)\MSSQLLocalDB - Authentication: Use
Windows Authentication - Database: Click
Browse…and select the.mdffile from theApp_Datafolder:- For the main database:
App_Data\MvcMusicStore.mdf - For the identity database:
App_Data\aspnet-MvcMusicStore-20120831200627.mdf
- For the main database:
- Data Source: Select
-
Confirm and Connect
- Click
OKto connect - Visual Studio will automatically attach the
.mdffile to LocalDB - You'll see the database listed under
Data Connectionsin Server Explorer
- Click
The MVC4 version includes a SQL script file (MvcMusicStore-Create.sql) that creates the database schema and populates it with sample data. This is an alternative approach for setting up the database.
Note: For most users, the automatic database initialization (recommended above) is simpler and sufficient. Use this SQL script approach only if you need to create a named database instance or have specific database setup requirements.
-
Open the Solution
- Open
MvcMusicStore.slnin Visual Studio 2022
- Open
-
Open Server Explorer
- Go to
View>Server Explorer(or pressCtrl+Alt+S)
- Go to
-
Create a Data Connection
- Right-click
Data Connections>Add Connection… - Data Source: Select
Microsoft SQL Server (SqlClient) - Server Name: Enter
(LocalDB)\MSSQLLocalDB - Authentication: Use
Windows Authentication - Database name: Click
Test Connectionto verify the connection - Click
OK
- Right-click
-
Execute the SQL Script
- In Solution Explorer, right-click on
MvcMusicStore-Create.sql - Select
Open With...>SQL Query Editor(or use any SQL query editor) - Alternatively, double-click the file to open it in the SQL editor
- Update the
USEstatement at the top of the file to point to your LocalDB instance:-- Replace the existing USE statement with: CREATE DATABASE MvcMusicStore; GO USE MvcMusicStore; GO
- Click
Executeor pressCtrl+Shift+Eto run the script - The script will create all tables (Albums, Artists, Genres, Orders, OrderDetails, Carts) and populate them with sample data
- In Solution Explorer, right-click on
-
Update the Connection String
- After creating the database using the SQL script, you need to update the connection string in
Web.configto use the database you just created. - Find the
MusicStoreEntitiesconnection string inWeb.configand replace it with:<add name="MusicStoreEntities" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=MvcMusicStore;Integrated Security=True" providerName="System.Data.SqlClient" />
Note: This connection string uses
Initial Catalog=MvcMusicStoreto connect to the database you created with the SQL script. If you're not using Visual Studio 2022, replace(LocalDB)\MSSQLLocalDBwith(LocalDB)\v11.0. - After creating the database using the SQL script, you need to update the connection string in
The application uses the following connection strings (configured in Web.config):
-
MusicStoreEntities: Main application database for albums, artists, and genres
Data Source=(LocalDb)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\MvcMusicStore.mdf; Integrated Security=True -
DefaultConnection: SimpleMembership database for user authentication
Data Source=(LocalDb)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\aspnet-MvcMusicStore-20120831200627.mdf; Integrated Security=True
Note: Replace
(LocalDB)\MSSQLLocalDBwith(LocalDB)\v11.0if you're not using Visual Studio 2022, or if you encounter compatibility issues with MSSQLLocalDB.
If you're using the SQL script approach (alternative method above), you'll need to use a different connection string format that references the database by name:
<add name="MusicStoreEntities"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=MvcMusicStore;Integrated Security=True"
providerName="System.Data.SqlClient" />If you encounter an error about LocalDB not being found:
- Verify LocalDB is installed by running in Command Prompt:
sqllocaldb info - If not installed, install it via Visual Studio Installer > Modify > Individual Components > SQL Server Express LocalDB
- If you see v11.0 is not available, update your connection strings to use
(LocalDB)\MSSQLLocalDB
If you experience database connection issues:
- Delete the
.mdfand.ldffiles from theApp_Datafolder - Rebuild and run the application - the database will be recreated automatically
If the database is already attached to another instance:
- Detach it from Server Explorer (right-click the connection > Delete)
- Or use command line:
sqllocaldb stop MSSQLLocalDBandsqllocaldb start MSSQLLocalDB
If you encounter errors when running the SQL script (alternative method):
- Make sure the
USEstatement at the top of the file is updated correctly - Ensure you have permissions to create databases on your LocalDB instance
- Check that no other instance of the application is using the database
- Try running the script sections one at a time to identify which part is failing
- Open
MvcMusicStore.slnin Visual Studio - Press
F5to build and run the application - The application will open in your default browser
- Browse the music store, add items to cart, and test the checkout process
- Register a new account or login to test the authentication features
For more information about ASP.NET MVC 4, visit Microsoft Learn.