1 / 16

Introduction to MVC 4 04. Adding Model Classes

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

jerica
Download Presentation

Introduction to MVC 4 04. Adding Model Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to MVC 404. 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) maps to a row Class (object) Property xxx Property xxx Property xxx . . . Property xxx Properties map to Columns in the database

  6. Add Model Classes Right-Click Select Select

  7. Name the class Click

  8. Add 5 properties to the Movie class … public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTimeReleaseDate { 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

  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<Movie> 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 DateTimeReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDBContext : DbContext { public DbSet<Movie> 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 <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" />

  15. 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" />

  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

More Related