Summary of ASP. NET MVC5 official tutorial (5) use SQL Server LocalDB to create a connection string, mvc5localdb

Source: Internet
Author: User
Tags sql server express

Summary of ASP. NET MVC5 official tutorial (5) use SQL Server LocalDB to create a connection string, mvc5localdb
Summary of ASP. NET MVC5 official tutorial (5) use SQL Server LocalDB to create a connection string

In the previous chapter, we created the MovieDBContext class to connect to the database and process the ing between Movie objects and database records. However, we do not specify the database to be used or the database to be used. In fact, when we do not specify a database, Entity Framework uses LocalDB by default.

This section describes how to add a database connection to the Web. config file.

LocalDB is a lightweight version of the SQL Server Express database engine. LocalDB runs in a special Execution Mode of SQL Server Express and allows you to use database files (such as. mdf files ). Generally, the LocalDB database file is placed in the App_Data folder of the solution.

SQL Server Express is not recommended for web application products. To be precise, LocalDB cannot be used in web application products, this is because it is not designed to be used with IIS. However, a LocalDB database can be easily migrated to SQL Server or SQL Azure.

In Visual Studio 2013 (or 2012), Visual Studio installs LocalDB by default.

By default, Entity Framework searches for database connections with the same name as the object context class (MovieDBContext in this project ).

Open the Web. config file in the application root directory (not the Web. config file in the Views directory ). The file location is as follows:

After opening the file, find the connectionString node. Here, the environment has prepared 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 to the connectionString node:

<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"      providerName="System.Data.SqlClient"/>

The name of the database connection string must be the same as that of the DbContext class. Because our DbContext name is MovieDBContext, the database connection string name is also MovieDBContext.

In fact, you do not need to add a MovieDBContext connection string. If you do not specify a connection string explicitly, Entity Framework will create a LocalDB database in the user directory, the name is the full name of the DbContext class (in this example, the file name is MvcMovie. models. movieDBContext ).

Then we can write the connection string.

 

 

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.