Add a model (ASP.net MVC3 series article IV)

Source: Internet
Author: User
Tags compact connectionstrings visual studio 2010

In-situ: Http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part4-cs

This tutorial will use the Microsoft Visual Web Developer Express Service Pack to teach you to build an MVC Web application based on ASP.net. Before you begin, make sure that the prerequisites listed below are already installed. You can download them by clicking on the following links: Web Platform Installer. Or you can use the link below to install a single installation: Visual Studio Web Developer Express SP1 Prerequisites asp.net MVC 3 Tools Update SQL Server Compact 4.0 (Runtime + tools support)

If you are using Visual Studio 2010, you can click the next link to install these prerequisites: Visual Studio Prerequisites.

C # will be used throughout this visual Web developer project. Download the C # version. If you are better at VB, you can change this tutorial to VB Visual Basic version to add a model

In this section, you will add some classes to store the movie in the database. These classes will become part of the model in the ASP.net MVC application.

You will be using one. NET data storage technologies, such as the Entity Framework, to define and run entity classes. This entity Framework (often referred to as EF) supports a development paradigm called code one. Code a allows you to write simple classes to create entity objects. (This is also known as the Poco class, from a simple CLR object) that provides a fast development process for writing neat classes and then building to a database. Add Model class

Right-models the folder in Solution Explorer , select Add, and then select Class.

The name of the class is named "Movie".

Add 5 attributes to the movie class, as follows:

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 use the movie class to represent manipulating the movie in the database. Each instance of the Movie object matches a row in the database table, and each property in the Movie class maps the columns in the table.

In the same file, add the following Moviedbcontext class:

public class Moviedbcontext:dbcontext  
{public 
    dbset<movie> Movies {get; set;}  
}

This Moviedbcontext class represents the context of the Entity Framework database for querying, storing, and modifying movie class database instances. This moviedbcontext class derives from the DbContext base class, and for more dbcontext and dbset information, see productivity improvements for the Entity Framework.

In order to use DbContext and dbset, you need to add the following statement to the head of the file:

Using System.Data.Entity;

The completed Movie.cs is shown below.

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 the SQL Server Compact

The Moviedbcontext class you create is used to connect to the database and to map moive objects to database records. There is a question you may ask, how to connect to the database, you can add a piece of connection information to the Web.config file in the current application.

Open the Web.config file in the application root directory. (Note that there is also a web.confige file under the Views folder.) The following picture shows two web.config files: Open a Web.config file marked with a red circle.

 

In the Web.config file, add the following connection string to the <connectionStrings> tab:

<add name= "Moviedbcontext"  
         connectionstring= "Data source=| datadirectory| Movies.sdf "  
         providername=" system.data.sqlserverce.4.0 "/>

The following example shows a portion of the web.confg file after adding a new connection string:

<configuration> 
  <connectionStrings> 
    <add name= "Moviedbcontext"  
         connectionstring= " Data source=| datadirectory| Movies.sdf "  
         providername=" system.data.sqlserverce.4.0 "/> 
    <add 
         name=" ApplicationServices " connectionstring= "Data source=.\sqlexpress;integrated Security=sspi; attachdbfilename=| Datadirectory|aspnetdb.mdf; User instance=true " 
         providername=" System.Data.SqlClient/> 
  </connectionStrings>

In order to manipulate movie data in a database, these small amounts of code need to be written.

Next time, you'll build a new Moviescontroller class that you can use to display movie data and allow users to create movie listings.

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.