[ACCEPTED]-ADO.NET |DataDirectory| where is this documented?-datadirectory
|DataDirectory|
is a substitution string so you can configure 2 the location of your database file separately.
So 1 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");
In the MSDN social forums this answer can be 16 found
|DataDirectory| (enclosed in pipe symbols) is 15 a substitution string that indicates the 14 path to the database. It eliminates the 13 need to hard-code the full path which leads 12 to several problems as the full path to 11 the database could be serialized in different 10 places. DataDirectory also makes it easy 9 to share a project and also to deploy an 8 application.
For example, instead of having 7 the following connection string:
"Data Source= c:\program files\MyApp\Mydb.sdf"
Using DataDirectory, you 6 can have the following connection string:
“Data Source = |DataDirectory|\Mydb.sdf”
To 5 set the DataDirectory property, call the 4 AppDomain.SetData method. If you do not 3 set the DataDirectory property, the following 2 default rules will be applied to access 1 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.
Incorrect guys! The |DataDirectory| refers 13 to the mssql\data directory your instance 12 is configured for.
So for example I am using 11 Visual Studio 2012 inconjunction with SQL 10 Express. |DataDirectory| places all MDF 9 files under C:\Program Files\Microsoft SQL 8 Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA 7 where my sql express was installed not my 6 solutions app_data folder.
Also the file 5 is names MVCMovie.Models.MovieDBContext 4 not Movies.mdf as specified in my web.config.
I'm 3 thinking it needs to be configured somewhere 2 in visual studio for it to be placed appropriately 1 under app_data.
There is an internal class called SqlConnectionHelper 3 which parses this and creates the MDF if 2 needed.
Here's the only MS doc I can find 1 about that class and the |DataDirectory| macro: http://msdn.microsoft.com/en-us/library/aa478948.aspx.
http://msdn.microsoft.com/en-us/library/aa478948.aspx
The |DataDirectory| portion of the connection 2 string specifies that the MDF file is located 1 in the App_Data directory.
This might be relevant if you're using code 9 first migrations.
With VisualStudio 2013 8 (at least) when running an Update-Database 7 command the data directory is relative from 6 the "Startup Project" currently configured 5 in visual studio.
Even if you run the Update-Database 4 on another project (as selected on the Package 3 Manager Console), it will create your database 2 on the App_Data of the currently selected 1 Startup Project.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.