ASP. NET MVC4.0 Official tutorial Getting Started Guide four--Add a model

Source: Internet
Author: User

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

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.