Learn about ASP. NET MVC5 Official Tutorial Summary (iv) ADD model

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.