Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path reference in WebConfig.ConnectionString

Tags:

c#

sql-server

Is it possible to specify a relative path reference in connectionstring, attachDbFileName property in a web.config?

For example, In my database is located in the App_data folder, I can easily specify the AttachDBFilename as|DataDirectory|\mydb.mdf and the |Datadirectory| will automatically resolve to the correct path.

Now, suppose that web.config file is located in A folder, but the database is located in B\App_data folder, where A and B folder is located in the same folder. Is there anyway to use relative path reference to resolve to the correct path?

like image 953
Graviton Avatar asked Sep 24 '08 02:09

Graviton


2 Answers

I had the same problem with the following scenario: I wanted to use the same database as the application from my integration tests.

I went with the following workaround:

In the App.config of my test-project I have:

<appSettings>
  <add key="DataDirectory" value="..\..\..\BookShop\App_Data\"/>
</appSettings>

In the test-setup I execute the following code:

   var dataDirectory = ConfigurationManager.AppSettings["DataDirectory"];  
   var absoluteDataDirectory = Path.GetFullPath(dataDirectory);  
   AppDomain.CurrentDomain.SetData("DataDirectory", absoluteDataDirectory);  
like image 181
jbandi Avatar answered Oct 04 '22 18:10

jbandi


It depends on where your '|DataDirectory|' is located. If the resolved value of '|DataDirectory|' is in folder A (where the web.config is), then no - you can't specify a relative path that is not a subfolder of the resolved value of '|DataDirectory|'.

What you can do is set the value of '|DataDirectory|' to be wherever you would like, by calling the AppDomain.SetData method.

From the MSDN online documentation:

When DataDirectory is used, the resulting file path cannot be higher in the directory structure than the directory pointed to by the substitution string. For example, if the fully expanded DataDirectory is C:\AppDirectory\app_data, then the sample connection string shown above works because it is below c:\AppDirectory. However, attempting to specify DataDirectory as |DataDirectory|..\data will result in an error because \data is not a subdirectory of \AppDirectory.

Hope this helps.

like image 33
Jared Avatar answered Oct 04 '22 20:10

Jared