c# - 如何从 hibernate.cfg.xml 文件中获取连接字符串值?

标签 c# asp.net nhibernate fluent-nhibernate

我正在使用 Fluent NHibernate,需要从 hibernate.cfg.xml 文件的 connection.connection_string 属性中获取我的连接字符串以创建我的 session 工厂:

private static ISessionFactory SessionFactory {
   get {
      return = Fluently.Configure()
         .Database(MySQLConfiguration.Standard.ConnectionString(c => c.FromConnectionStringWithKey("MyConnStr")))
         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<FooMap>())
         .ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"))
         .BuildSessionFactory();
   }
}

我想为 hibernate.cfg 中的连接字符串替换 MyConnStr(在我的 web.config 文件中)“c => c.FromConnectionStringWithKey("MyConnStr")” .xml 文件。

我试过使用 NHibernate.Cfg.Environment.ConnectionString,但没有用。

我怎样才能得到这个?

谢谢。

最佳答案

试试这个

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure();
string conString = cfg.Configuration.GetProperty(NHibernate.Cfg.Environment.ConnectionString);

关于c# - 如何从 hibernate.cfg.xml 文件中获取连接字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432628/

相关文章:

visual-studio-2010 - NHibernate 从 SymbolSource.org 加载符号

c# - 在 C# 中控制或访问打印机的自定义打印机设置(旋转 180 度)

c# - 有没有办法在另一种形式上显示一种形式的一部分?

c# - 有没有办法让 WPF 应用程序在 Windows 10 中少 "ugly"?

c# - 如何使用反射通过构造函数强制转换为对象?

c# - 限制引用实体的 NHibernate 查询

c# - visual c# 表单更新导致闪烁

c# - SQL Server 的 NHibernate 超时

c# - 输入字符串的格式不正确 C# ASP.NET

nhibernate - 即使组件的所有属性都为空,我如何告诉 NHibernate 将组件加载为非空?