Go ASP. 5-Create a connection string (Connection string) and use SQL Server LocalDB

Source: Internet
Author: User
Tags sql server express connectionstrings

The Moviedbcontext class that you create is responsible for handling the tasks of connecting to the database and mapping the movie objects to database records. You may ask a question, how do you specify that it will connect to the database? In fact, you do not specify a database to use, and the Entity framework will use the default localdb. In this section, we will explicitly add the application's connection string (connection string) to the Web. config file.

SQL Server Express LocalDB

LocalDB is a SQL Server Express lightweight version of the database engine. It starts and executes in user mode. LocalDB runs in a special SQL Server Express execution mode, so allows you to use the MDF file database. Typically, LocalDB database files are saved under the App_Data folder of the Web project.

Note: in a production environment Web application, we do not recommend that you use SQL Server Express. In particular, LOCALDB should not be used in a production environment for Web applications because IISis not required to be used at the beginning of its design. However, the LOCALDB database can be easily migrated?? to SQL Server or SQL Azure.

Note: in Visual Studio (Visual Studio), LocalDB is installed by default.

By default, the Entity framework appears to be named the same as a connection string for an object context class (such as this project Moviedbcontext). For more information, see SQL Server Connection Strings for ASP. NET WEB applications.

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.

Find <connectionStrings>:

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 "    providername=" System.Data.SqlClient "/>

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; attachdbfilename=| Datadirectory|\aspnet-mvcmovie-20130603030321.mdf;initial catalog=aspnet-mvcmovie-20130603030321;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 "/>

The two connection strings are very similar. The first connection string, named DefaultConnection, is used to control the member authentication database that can access the application. The connection string that you have added (connection string) displays a Movie.mdf file that is located in the App_Data folder, and the database is named movie.mdf. In this tutorial, we will not use the member database for more information about membership, authentication and security, see Tutorial: Deploy a Secure ASP. NET MVC app with membership, OAuth, and SQL database to a Wi Ndows Azure Web Site.

The name of the connection string (connection string) must match the name of the DbContext 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;}}    }

In fact, you don't need to add a new Moviedbcontext connection string. If you do not specify a connection string, the Entity framework creates a DbContext class for the LOCALDB database in the user directory (as in this example, MvcMovie.Models.MovieDBContext). You also name the database for anything you like, as long as it has . the suffix of the MDF. For example, we can name the database myfilms.mdf. Through this section of the Connection database knowledge learning, we may wish to apply this knowledge to the actual development of MVC. Of course, don't forget to use some development tools. With ComponentOne Studio ASP, this lightweight control can help your MVC development and greatly reduce the amount of work you have to develop while developing more efficiently.

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.


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-Create a connection string (Connection string) and use SQL Server LocalDB

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.