Learning ASP. NET MVC5 Official Tutorial Summary (v) Creating a connection string using SQL Server LocalDB

Source: Internet
Author: User
Tags connectionstrings

Learning ASP. NET MVC5 Official Tutorial Summary (v) Creating a connection string using SQL Server LocalDB

In the previous chapter, we created the Moviedbcontext class to connect to the database, to manipulate the mapping of the movie object and the database record. However, we did not specify which database to use or which database to use. In fact, when we do not specify a database, theEntity Framework uses LocalDB by default .

In this section, we will show you how to add a database connection in the Web. config file.

        localdb  Yes SQL SERVER EXPRESS  Lightweight version of the database engine, LOCALDB  run in Span style= "Font-family:times New Roman" >SQL SERVER EXPRESS  Special execution mode allows you to use database files (such as   .MDF  file". Typically, LOCALDB  The database file is placed in the   of the solution; app_data   folder.

        sql server express  is not recommended for use in   WEB  In the application product, it is accurate to say, LOCALDB  WEB  Application products, because it was designed without consideration and   IIS  used together. However, a localdb database can be easily migrated to SQL SERVER  or &NBSP, sql azure

In Visual Studio(or ),Visual Studio installs the default LocalDB .

By default, theEntity Framework looks for a database connection with the same name as the object context class (Moviedbcontext in this project ).

Open the Web. config file for the application's root directory (not in the Views directory, web. config). File locations such as:

After opening the file, locate the ConnectionString node, where the environment has already written an example for us:

<connectionStrings>    <add name= "defaultconnection" connectionstring= "Data source= (LocalDb) \v11.0; attachdbfilename=| Datadirectory|\aspnet-mvcmovie-20150428033206.mdf;initial catalog=aspnet-mvcmovie-20150428033206;integrated Security=true "      providername=" System.Data.SqlClient "/>  </connectionStrings>

Add the following connection string below the connectionString node:

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

The database connection string must have the same name as the DbContext class. Because the name of our dbcontext is called Moviedbcontext, the name of the database connection string here is also moviedbcontext.

In fact, you do not need to add a moviedbcontext Connection string, and if you do not explicitly specify a connection string,theEntity Framework creates a LocalDB Database, the name is The full name of the DbContext class (in this example the file name is MvcMovie.Models.MovieDBContext ).

So we can write the connection string.

Learning ASP. NET MVC5 Official Tutorial Summary (v) Creating a connection string using SQL Server LocalDB

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.