Summary of ASP. NET MVC5 official tutorial (4) Add a model,. netmvc5

Source: Internet
Author: User

Summary of ASP. NET MVC5 official tutorial (4) Add a model,. netmvc5
Summary of ASP. NET MVC5 official tutorial (4) Add a model

In the previous chapters, we learned how to create and use "C" and "V" in MVC, in this chapter, let's take a look at "C"-model creation method.

We will add some classes for managing movie databases, which play the "Model" role in ASP. net mvc applications.

We will use the Entity Framework Data Access Technology familiar to the. NET Framework Platform to define and use these models. Entity Framework (EF) provides a development example called Code First.

Code First allows you to write some simple classes to create models (these are usually called POCO classes, that is, "plain old CLR object "). These classes will generate databases, which is a simple and fast development process.

In our project, there is a folder named Models, and our class is put here. Right-click the folder and select the class in the Add option.

 

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 movies in the database. Each Movie object maps 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: MovieDBContext in this file.

The MovieDBContext class represents the database context of the Movie class in the Entity Framework. It is used to obtain, store, and update instances of the Movie class in the database. The MovieDBContext class inherits from the DbContext class provided in the Entity Framework.

First, we need to add the reference of System. Data. Entity, and then write the MovieDBContext class under the Movie class. The 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; }    }}


In this way, our model is ready. In the next chapter, we will explain how to use a database to connect strings.

 

 

 

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.