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