本文介绍了在WebConfig.ConnectionString相对路径参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能指定的ConnectionString attachDbFileName属性在web.config中的相对路径参考?

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

例如,在我的数据库位于App_Data文件夹,我可以很容易地指定AttachDBFilename为| DataDirectory目录| \mydb.mdf和| DataDirectory目录|将自动解析到正确的路径。

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.

现在,假设web.config文件位于一个文件夹,但数据库位于B\App_data文件夹,其中A和B文件夹位于同一个文件夹中。 ?反正有使用相对路径引用解析到正确的路径。

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?

推荐答案

我有以下情形同样的问题:我想用相同的数据库从我的集成测试的应用程序

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

我用以下解决方法了:

在我的测试项目的App.config中我有:

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

这篇关于在WebConfig.ConnectionString相对路径参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 07:15