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 folder, select Add , and then select class .

Enter the Class name "Movie".

Add the following five attributes to a Movie class:

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   Movie of the database table. Each property of the class corresponds to a column of the table.

In the same file, add the following MovieDBContext class:

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 base class of the Entity Framework DbContext .

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;

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

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;}}     }

Create a connection string and use SQL Server LocalDB

The class you just created is used to connect to the MovieDBContext database and Movie map objects to database table records. 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 <connectionStrings> following connection string inside the Web. config file.

<add name= "Moviedbcontext"     connectionstring= "Data source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated Security=true "     

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

<connectionStrings>   <add name= "defaultconnection"         connectionstring= "Data source= (LocalDb) \v11.0 ; Initial catalog=aspnet-mvcmovie-2012213181139;integrated security=true "         providername=" System.Data.SqlClient "    />       <add name=" 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.

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.