DOCX

ASP.NET MVC Models and Data Access

By Lee Wells,2014-09-20 15:06
7 views 0
ASP.NET MVC Models and Data Access

Hands-On Lab

    ASP.NET MVC Models and Data Access

Lab version: 1.1.0

    Last updated: 9/20/2011

    Page 1

CONTENTS

    OVERVIEW ............................................................................................................................................. 3 EXERCISE 1: ADDING A DATABASE.................................................................................................... 6

    Task 1 Adding a Database.............................................................................................................. 6

    Task 2 Creating a Data Model ...................................................................................................... 13

    Task 3 Building the Application ................................................................................................... 18

    Task 4 Querying the Database ..................................................................................................... 20

    Task 5 Running the Application ................................................................................................... 21 EXERCISE 2: ADDING A DATABASE USING CODE FIRST ................................................................ 22

    Task 1 Adding a Database............................................................................................................ 23

    Task 2 Including Code First in the Solution and Connecting the Database .................................... 30

    Task 3 Working with the Model ................................................................................................... 32

    Task 4 Querying the Database ..................................................................................................... 36

    Task 5 Running the Application ................................................................................................... 38 EXERCISE 3: QUERYING THE DATABASE WITH PARAMETERS ..................................................... 39

    Task 1 Modifying StoreController to Retrieve Albums from Database .......................................... 39

    Task 2 Running the Application ................................................................................................... 40

    Task 3 Accessing Albums by Id ..................................................................................................... 41

    Task 4 Running the Application ................................................................................................... 42 SUMMARY ............................................................................................................................................ 42

    Page 2

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.

    Page 3

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 http://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: You can install the previous system requirements by using the Web Platform Installer

    3.0: http://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\CodeSnippets.vsi file.

    Page 4

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

    Page 5

Exercise 1: Adding a Database

    In this exercise, you will learn how to add a database with the tables of the MusicStore application to the solution in order to consume its data. Once adding the database and generating the Model it will represent, you will make the proper adjustments in the StoreController class to provide the View template with the data taken from the database instead of hard-coded one.

Task 1 Adding a Database

    In this task, you will add an already created database with the main tables of the MusicStore application to the solution.

    1. Start Microsoft Visual Web Developer 2010 Express from Start | All Programs | Microsoft

    Visual Studio 2010 Express | Microsoft Visual Web Developer 2010 Express.

    2. In the File menu, choose Open Project. In the Open Project dialog, browse to Source\Ex01-

    AddingADatabaseDBFirst\Begin, select MvcMusicStore.sln and click Open.

    3. Add an App_Data folder to the project to hold the SQL Server Express database files. App_Data

    is a special folder in ASP.NET which already has the correct security access permissions for

    database access. To add the folder, right-click MvcMusicStore project, point to Add then to Add

    ASP.NET Folder and finally click App_Data.

    Figure 2

    Adding an App_Data folder

    4. Add MvcMusicStore database file. In this hands-On Lab, you will use an already created

    database called MvcMusicStore.mdf. To do that, right-click the new App_Data folder, point to

    Page 6

Add and then click Existing Item. Browse to \Source\Assets\ and select the

    MvcMusicStore.mdf file.

Figure 3

    Adding an Existing Item

    Page 7

Figure 4

    MvcMusicStore.mdf database file

    The database has been added to the project. Even when the database is located inside the solution, you can query and update it as it was hosted in a different database server.

Figure 5

    MvcMusicStore database in Solution Explorer

    5. Verify the connection to the database. To do this, open the Database Explorer (CTRL+ALT+S),

    and then double-click the MvcMusicStore.mdf. The connection is established.

    Page 8

Figure 6

    Connecting to MvcMusicStore.mdf

    Note: If you get an error like the following, please follow the steps below.

    1. Open the Windows Services console. To do that, open the Run command from Start | All Programs | Accessories | Run, type services.msc and then click OK.

    Page 9

Figure 7

    Running services.msc

    2. Right-click the SQL Server (SQLEXPRESS) service and select Properties.

Figure 8

    SQL Server (SQLEXPRESS) service

    Page 10

Report this document

For any questions or suggestions please email
cust-service@docsford.com