配置文件中数据库文件的相对路径
我创建了一个 WCF REST 服务,它使用 nhibernate 连接到 sql server 精简版数据库。因此,我将 NHibernate 数据源配置为:
<property name="connection.connection_string">Data Source=[Path]\MyDb.sdf</property>
我现在遇到的烦恼是我不知道如何避免必须在配置中写出绝对路径。这很烦人,因为我将数据库文件作为项目的一部分保存在 App_Data 文件夹中。因此,我不必更新路径,例如当我将项目部署到另一个位置时,即使绝对路径不同。
使用 procmon 我注意到,如果我不在数据源配置中写入绝对路径,它将被解释为相对于路径: *C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\ 10.0*。
nhibernate 是否可能假设我们想要将路径与应用程序 bin 文件夹相关联(这是我的 App_Data/MyDb.sdf 的最终位置)?
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>
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.
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*.
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用:
|DataDirectory|
指向App_Data
文件夹。You should use:
|DataDirectory|
points to theApp_Data
folder.是否有任何理由将 NHibernate 配置放在 XML 文件中,而不是使用 NHibernate 配置接口以编程方式构建配置?
如果您有灵活性,我会这样做:
您需要的所有类都位于 NHibernate.Cfg 命名空间下。还有 Fluent NHibernate,它为构建配置提供了更简洁的界面。
希望有帮助!
Is there any reason why you have your NHibernate configuration in a XML file instead of building the configuration programmatically using the NHibernate configuration interface?
If you have the flexibility, this is how I'd do it:
All of the classes you need are under the NHibernate.Cfg namespace. There's also Fluent NHibernate, which provides a much cleaner interface for building your configuration.
Hope that helps!