2014-07-30 access to SQL Server databases in the MVC framework

Source: Internet
Author: User
Tags connectionstrings

Today is the 16th day of my internship. I am myself primarily learning about SQL Server database-related access when developing a system based on the MVC framework. The steps are as follows:

The first step is to create a class in the Models folder and name it as shown in movies.cs,1:

  

Figure 1

In the second step, enter the following code in namespace mvctest.models{} in the Movies.cs file above:

  

1      Public classMovie2     {3          Public intID {Get;Set; }4          Public stringTitle {Get;Set; }5          PublicDateTime ReleaseDate {Get;Set; }6          Public stringGenre {Get;Set; }7          Public decimalPrice {Get;Set; }8     }9 Ten      Public classMoviedbcontext:dbcontext One     { A          PublicDbset<movie> Movies {Get;Set; } -}

where each instance of the movie object corresponds to each row of the database table movies, each property corresponds to the corresponding field of the database table movies. Moviedbcontext is a database context (that is, the database environment) of the EF (Entity Framework) that is derived from the base class DbContext.

In the third step, add the following connection string in the Web. config <connectionStrings></connectionStrings> in the project root directory:

  

1  <add name="moviedbcontext"2        connectionstring="Data Source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated security=true"3        providername=" System.Data.SqlClient " 4  

Among them, | Datadirectory| represents the App_Data folder under the project root, which is automatically generated when movies.mdf is not present in this folder. The System.Data.SQLClient description uses the SQL Server database.

Fourth step, add a controller Moviescontroller in the Controllers folder, the controller's template, model class, data context Class 2, as shown:

Figure 2

Once added, MVC will automatically create a CRUD (create, read, update, and Delete) action methods and views for you (create.cshtml,delete.cshtml,details.cshtml,edit.cshtml, and index.cshtml), 3:

Figure 3

In this way, the system will be able to achieve the basic additions and deletions to the search, is not it feel very magical, at first I also feel very strange.

Unfortunately, because MVC is a three-tier architecture, the internal principles are complex, so it takes some time to think before designing the system. Plus, its multi-use new view engine razor can't drag and drop controls, so it's a challenge for people who aren't very familiar with HTML. Later, because we want to make the mobile phone-side BBS is a very small system, so the manager still suggest that we use the common development model for the development of more appropriate, not easy to cause the loss of the candle. Therefore, we decided to use the common development model for development.

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.