ASP. NET MVC4.0 Official tutorial Getting Started Guide four--Add a model
In this section, you will add classes for managing movies in the database. These classes are part of the model of an ASP. NET MVC application.
You will work with model classes using the Entity Framework data access technology under the. NET Framework framework. The Entity Framework (often referred to as EF) supports a development model called code first. Coding first allows you to write simple classes (called Poco classes, all referred to as "Plain-old CLR objects.") ) to create a model object. You can use the class to create a database in real time, making the development process clean and fast.
To add a model class
In Solution Explorer, right-click the Models folder, select Add, and then select Class to enter the class name Movie.
Add the following five properties to the movie class:
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;Usingsystem.web;Namespacemvcmovie.models{Publicclass Movie {public int ID {get; set;} public string Name {get; setpublic string genra {get; setpublic decimal price {get; setpublic DateTime Date {get; set
We will use the movie class as the movie data in the database. Each instance of a movie object corresponds to a row in a database table, and each property of the movie class is mapped to a column in the table.
In the same file, add the following Moviedbcontext class:
Class Moviedbcontext:dbcontext { set;}}
The Moviedbcontext class represents the movie database content of the Entity Framework and is responsible for processing instances of the database for fetching, storing, and updating the movie class. Moviedbcontext inherits from the base class DbContext provided by the Entity Framework.
To be able to use DbContext and dbset, you need to add the following using statement at the top of the file:
Using System.Data.Entity;
Next, you'll build a new Moviescontroller class that you can use to display the movie data and allow the user to create a new movie list.
ASP. NET MVC4.0 Official tutorial Getting Started Guide four--Add a model