ADO.NET |DataDirectory| where is this documented?
Solution 1:
|DataDirectory|
is a substitution string so you can configure the location of your database file separately.
So instead of:
SqlConnection c = new SqlConnection (
@"Data Source=.\SQLDB; AttachDbFilename=C:\MyDB\Database.mdf;Initial Catalog=Master");
you do the following:
// Set |DataDirectory| value
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\myDB");
// SQL Connection String with |DataDirectory| substitution string
SqlConnection c = new SqlConnection (
@"Data Source=.\SQLDB; AttachDbFilename=|DataDirectory|\Database.mdf;Initial Catalog=Master");
Solution 2:
In the MSDN social forums this answer can be found
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
For example, instead of having the following connection string:
"Data Source= c:\program files\MyApp\Mydb.sdf"
Using DataDirectory, you can have the following connection string:
“Data Source = |DataDirectory|\Mydb.sdf”
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
- For applications that are put in a folder on the user's computer, the database folder uses the application folder.
- For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.
Solution 3:
Incorrect guys! The |DataDirectory| refers to the mssql\data directory your instance is configured for.
So for example I am using Visual Studio 2012 inconjunction with SQL Express. |DataDirectory| places all MDF files under C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA where my sql express was installed not my solutions app_data folder.
Also the file is names MVCMovie.Models.MovieDBContext not Movies.mdf as specified in my web.config.
I'm thinking it needs to be configured somewhere in visual studio for it to be placed appropriately under app_data.