04. Add Model Classes (Data)

Download Report

Transcript 04. Add Model Classes (Data)

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
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
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
Entity Framework – CodeFirst
Paradigm
Class
Class (object)
Property xxx
Property xxx
Property xxx
...
Property xxx
Database
Class (object) maps to a row
Properties map to
Columns in the database
Add Model Classes
Right-Click
Select
Select
Name the
class
Click
Add 5 properties to the Movie class …
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; }
}
• Each object corresponds to a row in the table
• Each property corresponds to a column in the
table
Add the MovieDBContext class to the
same Movie.cs file
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
• 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
Add the following using statement to
the top of the file
using System.Data.Entity;
• Needed to reference (later)
– DbContext
– DbSet
The complete Movie.cs file
using 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; }
}
}
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.
Open the application root Web.config
file
Click
Add the following connection string to the
<connectionStrings> element in the Web.config
file:
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
• Expanded view of connection string literalEnter 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"
/>
The resulting Web.config file:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnetMvcMovie-2012213181139;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"
/>
</connectionStrings>
Now, Build the application and correct any errors …
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