从nHibernate获取连接字符串将返回不带密码的字符串

Rap*_*ael 3 c# nhibernate hibernate

我需要在nhibernate应用程序上直接访问数据库.我试图获得这样的连接字符串:

    using (SqlConnection conn = new SqlConnection(this.session.Connection.ConnectionString))
Run Code Online (Sandbox Code Playgroud)

但是这将返回没有密码的连接字符串.

我做错了什么?有没有其他方法来获取连接字符串?

Jul*_*anM 7

您可以从NHibernate SessionFactory获取原始数据库连接(使用与NHibernate相同的连接详细信息),例如:

IDbConnection connection = yourSessionFactory.ConnectionProvider.GetConnection()

  • ADO.NET不知道从哪里获取连接字符串. (5认同)
  • 您还需要将`SessionFactory`转换为具体实现`(NHibernate.Impl.SessionFactoryImpl)`以访问`ConnectionProvider`属性(至少在NH v3中). (5认同)