Go ASP. NET MVC 5-Add a model

Source: Internet
Author: User

In this section, you will add classes that are 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. The Entity Framework (often called EF) is a development model that supports code first. Code first allows you to create an object model by writing a simple class. (as opposed to "original CLR objects," which is also known as a Poco Class) then, you can 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 Tom Fizmakens ASP. NET scaffolding tutorial, which covers the first way to create a database.

To add a model class

In Solution Explorer , right-click the Model folder, select Add , and then select class .

Enter the Class name "Movie".

Add the following five attributes to a Movie class:

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'll use Movie classes to represent the movies in the database. Movieeach instance of the object corresponds to a row of the database table, and  Movie each property of the class corresponds to a column of the table.

In the same file, add the following MovieDBContext class:

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

MovieDBContextclass represents the Movie Database class for the Entity Framework, which is responsible for retrieving, storing, updating, and processing instances of the class in the database Movie . MovieDBContextinherits from the DbContext base class of the Entity Framework.

In order to be able to reference DbContext and DbSet , you need to add the following statement at the top of the file using :

Using System.Data.Entity;

You can do this by manually adding a using statement, or you can right-click the red wavy line, resolve (Resolve), and then clickUsing System.Data.Entity.

Note: Some of the unused using statements have been deleted-by right-clicking in the file, select organization using, and then click Remove Unused using.

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

With this section on how to add model knowledge to MVC, are you also tempted to start MVC development? The development process can also be assisted with some development tools. ComponentOne Studio ASP is a lightweight control that integrates seamlessly with visual Studio and is fully compatible with MVC6 and ASP. NET 5.0, which greatly increases development efficiency.

-----------------------------------------------------------------------------------------

The 12 articles in the ASP. NET MVC 5 Getting Started Guide are summarized below:

1. asp 5-Start MVC 5 Tour

2. ASP. NET MVC 5-Controller

3. ASP. NET MVC 5-View

4. asp 5-pass data from the controller to the view

5. ASP. 5-Add a model

6. asp 5-Create a connection string (Connection string) and use SQL Server LocalDB

7. ASP. 5-access the data model from the controller

8. ASP. 5-Validation editing method and edit view

9. asp 5-Add new fields to movie tables and models

ASP. NET MVC 5-Add a validator to the data model

ASP. NET MVC 5-query details and Delete methods

ASP. NET MVC 5-Create an app with the Wijmo MVC 5 template for 1 minutes

Go ASP. NET MVC 5-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.