ASP. net mvc 5-Add a model

Source: Internet
Author: User

In this section, you will add classes used to manage movies in the database. These classes are "models" in ASP. net mvc applications )".

You will use the. NET Framework Data Access Technology Entity Framework to define and use these model classes. Entity Framework (usually referred to as EF) is a development mode that supports Code First. Code first allows you to create an object model by writing simple classes. (Compared to the "original CLR objects", this is also called the POCO class), you can then create a database from your class, which is a very clean and fast development workflow. If you have to create a database first, you can still follow this tutorial to learn about MVC and EF application development. Then, you can follow the Scaffolding tutorial of Tom Fizmakens ASP. NET, which covers how to create a database first.

Add model class

InSolution Resource Manager, Right-clickModelFolder, selectAddAnd then selectClass.

InputClassThe name is "Movie ".

Add the following five attributesMovieClass:

using System;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 useMovieClass to represent movies in the database.MovieEach instance of the object corresponds to a row in the database table, MovieEach attribute of the class corresponds to a column in the table.

In the same file, add the followingMovieDBContextClass:

using System;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
 
   Movies { get; set; }    }}
 

MovieDBContextClass represents the Movie Database Class of Entity Framework. This class is used to obtain, store, update, and process data in the database.MovieClass.MovieDBContextDbContext base class inherited from Entity Framework.

To be able to referenceDbContextAndDbSetTo add the followingusingStatement:

using System.Data.Entity;

To do this, you can manually add the using statement, or you can right-click the red wavy line, "Resolve", and then click"Using System. Data. Entity.

Note: Some unnecessary using statements have been deleted-right-click the file and select"Organization UsingAnd then click"Remove unused using".

So far, we have added a model (M in MVC). In the next section, you will use the database connection string.

Related Article

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.