relative path for database file in config file

限于喜欢 提交于 2019-12-04 02:50:15

You should use:

Data Source=|DataDirectory|\MyDb.sdf

|DataDirectory| points to the App_Data folder.

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:

var path = // dynamically generate your path
var configuration = new Configuration();
configuration.SetProperty(Environment.ConnectionString, String.Format("Data Source={0};", path));
... // other configuration properties

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!