Setting up Entity Framework connection string in web.config
While I explain how the connection string and database provider information can be set in the web.config for Entity Framework, I’ll be assuming that you are aware of the differences between localdb, Sql Server Express and Sql Server Compact Edition.
If not you may checkout my earlier article here.
When you add the nuget package for Entity Framework in your project (I tried this out in Visual Studio 2015 for ASP.Net MVC 5), by default it adds support for the localdb data provider in the web.config file as follows…
The <parameter value="mssqllocaldb" />
here represents the default instance of SQL Server 2014.
For SQL Server 2012 we would have to replace this with <parameter value="v11.0" />
This represents the implicit way to declare the connection string.
We can however, declare this explicitly using…
If we wish to connect to a localdb database using file name we can use
Data Source=(LocalDB)\mssqllocaldb;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True
This connection string can be then accessed from with the Database context class as follows…
As can be seen in the code snippet above the name of the connection string is passed as a parameter in the constructor of the base DbContext class.
On similar lines, the connection string for
- SQL Server Express database in default instance
- SQL Server Express database in a .mdf file in the
App_Data
folder
- SQL Server (Full Edition) database using default SQL Server instance
- SQL Server (Full Edition) database using named SQL Server instance
- SQL Server CE
Note:
- The AttachDbFileName setting is typically used for database files that you keep in the
App_Data
folder. - The
App_Data
folder is a relatively secure place to store database files that you want to keep in the web application folder structure. - ASP.NET will not serve contents of the
App_Data
folder in response to Web requests, which helps to maintain the security of the data in database files kept in this folder. - ASP.NET automatically substitutes the file path to the
App_Data
folder for the|DataDirectory|
connection-string variable when it opens a connection to the database. - This ensures that the path to your database remains current if the application is moved to a different directory.
- If you don’t use the
|DataDirectory|
connection string variable, you have to provide the full physical path to the database file.
Leave a Comment