Please describe me about how we add connection string in web.config file within mvc with example and and image.
How to Create Connection string?
63322-Jan-2020
Updated on 22-Jan-2020
Home / DeveloperSection / Forums / How to Create Connection string?
Please describe me about how we add connection string in web.config file within mvc with example and and image.
Nishi Tiwari
23-Jan-2020Creating a Connection String
In the application, the class is mainly responsible for handling the task of connecting the database and to map the Class objects to database records. The Entity Framework automatically connects the database and uses it. The default database is LocalDB. Now, we are adding a connection string explicitly in the Web.Config file in an application.
Working with SQL Server Express LocalDB
The SQL Server Express Database Engine has the LocalDB. It is very lightweight which automatically starts on demand and runs in the user mode. SQL Server Express has the special execution mode in which LocalDB runs and which facilitates us to work with the database as .mdf files. The .mdf file of any database are stored in the App_data folder in the application.
We should not use the LocalDB in particular for production with a web application because it is designed to work with Internet Information Services (IIS). We can easily migrate our LocalDB database to SQL Server or SQL Azure. It is by default installed in Visual Studio 2012 and 2013 Preview versions of Visual Studio.
We can add a connection string to our app using the following procedure.
Step 1: Open our project
Step 2: In Solution Explorer open the Web.Config file
Step 3: Find the <connectionStrings> in the file.
Step 4: Add the following connection string to the <connectionStrings> after the previous connection string:
<add name="CricketerDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\Cricketers.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
Now there are two connection strings available in our Web.Config file. The connection string named DefaultConnection is used for the membership database and the connection string named CricketerDBConetext determines a LocalDB database which is located in the App_Data folder as Cricketers.mdf.
Note: we need to note that the connection string must match the name of DbContext class.