introduction to mvc 4 04. adding model classes ntpcug tom perkins, ph.d

16
Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D.

Upload: anthony-greer

Post on 26-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Introduction to MVC 404. Adding Model Classes

NTPCUGTom Perkins, Ph.D.

Page 2: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Classes built thus far …

• Controller – Handles incoming browser requests– Retrieves data from Model classes– Specifies View templates to return HTML to the

browser• View– Dynamically generates HTML requests

• Now … Model classes– Represents the data in the application– Use business logic to enforce business rules for data

Page 3: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Entity Framework (EF)

• .NET Framework data access technology• Code First paradigm – Create model objects by writing simple classes– Called “POCO” classes – “Plain Old CLR Objects”– Database is created “on the fly” from your classes

Page 4: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Creating Model Classes (Approach)

• Create a class to represent a Movie entity• A database will be created using the Movie

class to develop its schema• Each instantiation (object) of the class will

correspond to a row in a database• Properties in the class will correspond to

columns in the database

Page 5: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Entity Framework – CodeFirst Paradigm

Class Database

Class (object) Property xxx Property xxx Property xxx . . . Property xxx

Class (object) maps to a row

Properties map to Columns in the database

Page 6: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Add Model Classes

Right-Click

Select

Select

Page 7: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Name the class

Click

Page 8: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Add 5 properties to the Movie class …

• Each object corresponds to a row in the table• Each property corresponds to a column in the

table

public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; }}

Page 9: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Add the MovieDBContext class to the same Movie.cs file

• MovieDBContext class handles– Fetching Movie entities from database– Storing Movie entities into database– Updating Movie entities in database

• Derives from DBContext base class in Entity Framework

public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } }

Page 10: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Add the following using statement to the top of the file

• Needed to reference (later)– DbContext– DbSet

using System.Data.Entity;

Page 11: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

The complete Movie.cs fileusing System;using System.Data.Entity;

namespace MvcMovie.Models{ public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } }

public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } }}

Page 12: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Connecting to a local SQL Server Compact Database

• The MovieDBContext class just created handles:– Connecting to the database – Mapping Movie objects to database records

• How to specify which database to connect to?• Add connection information to Web.config file• (Note) – use Web.config in the website root,

not the Web.config in the Views folder.

Page 13: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Open the application root Web.config file

Click

Page 14: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Add the following connection string to the <connectionStrings> element in the Web.config file:

• Expanded view of connection string literal-Enter it with no spaces or carriage returns:

<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

Page 15: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

The resulting Web.config file:<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-2012213181139;Integrated Security=true" providerName="System.Data.SqlClient" />

</connectionStrings>

Now, Build the application and correct any errors …

<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

Page 16: Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D

Module summary …

• The Movies.cs class will– Represent Movie data– Store the Movie data in the database

• Next –– Create a MoviesController class that will• Display the Movie data• Allow users to create new Movie listings