ASP.NET MVC 3 Models and Data Access

Version: 1.1.0

Description

This Hands-on Lab will show you how to use the free SQL Server Express as a database engine in order to store and retrieve the data needed for the Music Store application. To accomplish that, you will start with an already created database, from which you will generate the Entity Data Model you will use in the application.

Overview

Note:
This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC. If you have not used ASP.NET MVC before, we recommend you to go over ASP.NET MVC Fundamentals Hand-on Lab.

In ASP.NET MVC Fundamentals Hand-on Lab, you have been passing hard-coded data from the Controllers to the View templates. But in order to build a real web application you might want to use a real database.

This Hands-on Lab will show you how to use the free SQL Server Express as a database engine in order to store and retrieve the data needed for the Music Store application. To accomplish that, you will start with an already created database from which you will create the Entity Data Model for the application. Through this lab, you will meet the Database First approach and the Code First Approach as well.

However, you could use a Model First approach, by creating the same model using the tools and then generating a database from it.

Figure 1

Database First vs. Model First

After generating the Model, you will make the proper adjustments in the StoreController to provide the Store Views with the data taken from the database instead of hard-coded one. You will not need to make any change to the View templates because the StoreController will be returning to the View templates the same ViewModels as before, although this time the data will come from the database.

The Code First Approach

The Code First approach allows us to define the model from the code without generating classes that are generally coupled with the framework.

In code first, Model objects are defined with POCOs , “Plain Old CLR Objects”. POCOs are defined as simple plain classes that have no inheritance and do not implement interfaces. We can automatically generate the database from them, or we can use an existing database and generate the class mapping from the code.

The benefit of using this approach is that the Model remains independent from the persistence framework (in this case, Entity Framework), as the POCOs classes are not coupled with the mapping framework.

Note:
 Although that this Hands-on Lab will cover the use of the free SQL Server Express, the code will also work with the full version of SQL Server.

This Lab is based on ASP.NET MVC 3.

If you wish to explore the whole Music Store tutorial application you can find it in https://mvcmusicstore.codeplex.com/.

System Requirements

You must have the following items to complete this lab:

  1. ASP.NET and ASP.NET MVC 3
  2. Visual Studio 2010 Express
  3. SQL Server Database (Express edition or above)

    Note:
    Note: You can install the previous system requirements by using the Web Platform Installer 3.0: https://go.microsoft.com/fwlink/?LinkID=194638.

Setup

Installing Code Snippets

For convenience, much of the code you will be managing along this lab is available as Visual Studio code snippets. To install the code snippets run .\Source\Assets\InstallCodeSnippets.cmd file.

Exercises

This Hands-On Lab is comprised by the following exercises:

  1. Exercise 1: Adding a Database
  2. Exercise 2: Adding a Database using Code First
  3. Exercise 3: Querying the Database with Parameters

Estimated time to complete this lab: 35 minutes.

Note:
Each exercise is accompanied by an End folder containing the resulting solution you should obtain after completing the exercises. You can use this solution as a guide if you need additional help working through the exercises.

Next Step

Exercise 1: Adding a Database