Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

c# - Relative path reference in WebConfig.ConnectionString

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

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

1 Reply

0 votes
by (71.8m points)

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);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...