Download presentation
Presentation is loading. Please wait.
Published byAnthony Greer Modified over 9 years ago
1
Introduction to MVC 4 04. Adding Model Classes NTPCUG Tom Perkins, Ph.D.
2
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
3
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
4
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
5
Entity Framework – CodeFirst Paradigm Class Database Class (object) Property xxx... Property xxx Class (object) maps to a row Properties map to Columns in the database
6
Add Model Classes Right-Click Select
7
Name the class Click
8
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; } }
9
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 Movies { get; set; } }
10
Add the following using statement to the top of the file Needed to reference (later) – DbContext – DbSet using System.Data.Entity;
11
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 Movies { get; set; } }
12
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.
13
Open the application root Web.config file Click
14
Add the following connection string to the 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" />
15
The resulting Web.config file: <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet- MvcMovie-2012213181139;Integrated Security=true" providerName="System.Data.SqlClient" /> Now, Build the application and correct any errors … <add name="MovieDBContext" <add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" providerName="System.Data.SqlClient" /> />
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.