ASP. MVC4 Getting Started Guide (4): Adding a model

Source: Internet
Author: User
Tags connectionstrings

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 pattern 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.

To add a model class

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

Enter the class name "Movie".

Add the following five attributes to the movie class:

 Public classMovie { Public intID {Get;Set; }  Public stringTitle {Get;Set; }  PublicDateTime ReleaseDate {Get;Set; }  Public stringGenre {Get;Set; }  Public decimalPrice {Get;Set; }}

We'll use the movie class to represent the movies in the database. Each instance of the movie object corresponds to a row of the database table, and each property of the movie class corresponds to a column of the table.

In the same file, add the following Moviedbcontext class:

 Public class Moviedbcontext:dbcontext {     publicgetset;}}

The Moviedbcontext class represents the movie Database class for the Entity Framework, which is responsible for fetching, storing, updating, and processing instances of the movies class in the database. Moviedbcontext inherits from the DbContext base class of the Entity Framework.

To be able to reference dbcontext and dbset, you need to add the following using statement at the top of the file:

Using System.Data.Entity;

The complete Movie.cs file is shown below. (Some unused using statements have been deleted.)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Data.Entity;namespacemvcmovie.models{ Public classMovie { Public intID {Get;Set; }  Public stringTitle {Get;Set; }  PublicDateTime ReleaseDate {Get;Set; }  Public stringGenre {Get;Set; }  Public decimalPrice {Get;Set; } }     Public classMoviedbcontext:D Bcontext { PublicDbset<movie> Movies {Get;Set; } }}

Create a connection string and use SQL Server LocalDB

The Moviedbcontext class that you just created is used to connect to the database and map the movie object to a database table record. You may ask a question, how to specify that it will connect to that database. Specify the connection to the database by adding the database connection information in the application's Web. config file.

Open the Web. config file for the application's root directory. (not a Web. config file under the View folder.) ) to open the red highlighted Web. config file.

Add the following connection string to <connectionStrings> inside the Web. config file.

<name= "Moviedbcontext"     connectionString= "Data source= ( LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated security=true "     providerName=" System.Data.SqlClient "  />

The following example shows the newly added connection string in a part of the Web. config file:

<connectionStrings>   <Addname= "DefaultConnection"connectionString= "Data source= (LocalDb) \v11.0;initial catalog=aspnet-mvcmovie-2012213181139;integrated security=true"ProviderName= "System.Data.SqlClient"    />       <Addname= "Moviedbcontext"connectionString= "Data source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated security=true "ProviderName= "System.Data.SqlClient"    /> </connectionStrings>

In order to represent and store the movie data into the database, the small amount of code and XML above is all you need.

Next, you will create a new Moviescontroller class that you can use to do this?? Show movie data and allow users to create new movie lists.

Full document Download: ASP. MVC4 Getting Started Guide. pdf

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

Translator Note:

This series of 9 articles, translated from the official ASP. NET MVC4 tutorial, because this series of articles concise, space moderate, from an example to explain, the full text finally completed a small system to manage the film, very suitable for the novice MVC4 ASP, and start the development work. 9 Articles for:

1. Introduction to ASP. MVC4

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html

2. Add a Controller

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/02/2751015.html

3. Add a View

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/06/2756711.html

4. Add a model

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/12/17/2821495.html

5. Accessing the data model from the controller

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/11/2855935.html

6. Validating editing methods and editing views

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/24/2874622.html

7. Add a new field to the movie table and model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ Adding-a-new-field-to-the-movie-model-and-table

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/02/26/2933105.html

8. Adding a validator to the data model

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/05/2944030.html

9. Query details and delete records

· Original address: Http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/07/2948000.html

10. Third-party control Studio for ASP. Wijmo MVC4 Tools App

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/05/09/3068699.html

ASP. MVC4 Getting Started Guide (4): Adding 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.