Learn about ASP. NET MVC5 Official Tutorial Summary (iv) ADD model
in the previous chapters, we learned about the creation and basic use of the "C" and "V" in MVC, which we look at "C"-the method of creating the model.
We'll add some classes that manage the movie database, which play the "Model" role in the ASP .
we will use platform well-known entity framework data access technology to define and use these models. entity framework (for short, Span style= "Font-family:verdana" >ef ) provides a kind of called code first Development paradigm.
Code First allows you to write simple classes to create models (these are often referred to as POCO classes, "plain Old CLR object" ). These classes will generate a database, which is a concise and fast development process.
Under our project, there is a folder called models, our class is placed here, right click on the folder, select the Add option in the class.
The name of the class is called movie. The code in the class is as follows:
Using system;using system.collections.generic;using system.linq;using system.web;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;}} }
We will use the movie class to represent the movies in the database. Each movie object will correspond to a row in the data table, and each field in the movie class maps each column in the data table.
Next we will add another class in this file: Moviedbcontext.
The Moviedbcontext class represents the database context for the Movie class in the Entity Framework , which handles retrieving, storing, and updating the database Movie the instance of the class. the Moviedbcontext class inherits from the DbContext class provided in the Entity Framework .
First we need to add A reference to the System.Data.Entity and then write the Moviedbcontext class underneath the movie class . The full code is as follows:
Using system;using system.collections.generic;using system.linq;using system.web;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;}} }
So our model is built, and in the next chapter we'll cover using the database connection string.
Learn about ASP. NET MVC5 Official Tutorial Summary (iv) ADD model