配置文件中数据库文件的相对路径 [英] relative path for database file in config file

查看:223
本文介绍了配置文件中数据库文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF REST服务,该服务使用nhibernate连接到SQL Server Compact Edition数据库。因此,我将NHibernate数据源配置为:

I've created a WCF REST service that uses nhibernate to connect to sql server compact edition database. Hence I configure the NHibernate datasource like:

<property name="connection.connection_string">Data Source=[Path]\MyDb.sdf</property>

我现在遇到的烦恼是我无法解决如何避免在配置中写出绝对路径。这很烦人,因为我将数据库文件作为项目的一部分保留在App_Data文件夹中。因此,我不必更新路径,例如当我将项目部署到另一个位置时,即使绝对路径不同。

The annoyance I'm running into now is that I can't figure out how to avoid having to write out the absolute path in the config. This is annoying since I keep the database file as part of the project in the App_Data folder. So I shouldn't have to update the path e.g. when I deploy the project to another location, even if the absolute path is different.

使用procmon我注意到,如果我没有在Data Source配置中编写绝对值pat,它将被解释为相对于路径:* C: \程序文件(x86)\公用文件\Microsoft共享\DevServer\10.0 *

Using procmon I noticed that if I don't write an absolute pat in the Data Source config, it is interpreted as relative to the path: *C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0*.

是否有可能让nhibernate假定我们想要将该路径关联到应用程序bin文件夹(这是我的App_Data / MyDb.sdf所在的位置)?

Is it possible have nhibernate assume we want to relate the path to the application bin folder instead (which is where my App_Data/MyDb.sdf ends up)?

推荐答案

您应该使用:

Data Source=|DataDirectory|\MyDb.sdf

| DataDirectory | 指向 App_Data 文件夹。

这篇关于配置文件中数据库文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆