Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 BApp_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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
445 views
Welcome To Ask or Share your Answers For Others

1 Answer

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="......BookShopApp_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);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...