ASP.NET MVC 3 Models and Data Access
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.
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.
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.
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 http://mvcmusicstore.codeplex.com/.
You must have the following items to complete this lab:
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.
This Hands-On Lab is comprised by the following exercises:
Estimated time to complete this lab: 35 minutes.
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.