http://stackoverflow.com/questions/4335827/changing-nhibernate-connectionstring

http://stackoverflow.com/questions/491139/how-do-you-change-nhibernates-connection-string-per-http-request

http://jasondentler.com/blog/2009/11/authentication-impersonation-and-dynamic-nhibernate-connection-strings

http://www.nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string.aspx

Dynamically change user info in connection string

In some cases our clients has to use the same database user id in each connection, so they can use audit and security features of their database system (and their DBAs will be happy [:)]).

To do that in Nh We can use the ConnectionProvider facility. Just derive a class from the standard DriverConnectionProvider class:

public class DynamicConnectionProvider : DriverConnectionProvider
{
 private string _connectionString;
 public override void Configure(IDictionary<string, string> settings)
 {
 
  // Connection string in the configuration overrides named connection string
  if (!settings.TryGetValue(NHibernate.Cfg.Environment.ConnectionString,out _connectionString))
 _connectionString = GetNamedConnectionString(settings);
 
  if (_connectionString == null)
  {
   throw new HibernateException("Could not find connection string setting (set " 
    + NHibernate.Cfg.Environment.ConnectionString + " or " 
    + NHibernate.Cfg.Environment.ConnectionStringName + " property)");
  }
  ConfigureDriver(settings);
 }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This is necessary because the original connection string is private, but just copy the code from base method.

The real magic is in "ConnectionString" property, it is called when nh has to connect in a Session. You have to override it so you can make the changes you need.

protected override string ConnectionString
{
 get { return FixConnectionString(_connectionString); }
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

In this case FixConnectionString read the user info from some environment variable and inject it in the connection string.

Finally configure NH to use the ConnectionProvider:

   <property name="connection.provider">
    MyAssembly.DynamicConnectionProvider, MyAssembly
   </property>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

最新文章

  1. Stars_树状数组
  2. 模拟时钟(AnalogClock)和数字时钟(DigitalClock)
  3. dedeCMS /plus/ad_js.php、/plus/mytag_js.php Vul Via Injecting PHP Code By /plus/download.php Into DB &amp;&amp; /include/dedesql.class.php
  4. 如何解决phpcms后台验证码不显示的问题
  5. python 练习 4
  6. python中的静态方法和类方法
  7. Android自定义组合控件
  8. VisualSvn Server安装和使用
  9. Java基础知识强化之集合框架笔记53:Map集合之Map集合的遍历 键值对对象找键和值
  10. [Search Engine] Compression in Inverted Index
  11. word2vec 中的数学原理具体解释(四)基于 Hierarchical Softmax 的模型
  12. DOM节点删除之empty和remove
  13. 导航栏底部黑线隐藏 UINavigationBar hidden Bottom Line
  14. Django Rest Framework(二)
  15. project 2013 设置工期为1个工作日,但开始时间与结束时间不是同一天
  16. 【pyqtgraph绘图】在pyqtgraph中绘图
  17. 20155211 Exp5 MSF基础应用
  18. Codeforces 923 D. Picking Strings
  19. [转]文件后缀与Mime类型对照表
  20. 谁应该在CCB(变更控制委员会)中?

热门文章

  1. 【Foreign】染色 [LCT][线段树]
  2. 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
  3. Swift : missing argument label &#39;xxx&#39; in call
  4. camera摄像原理之二:色彩空间【转】
  5. doxygen使用
  6. selenium+python自动化82-只截某个元素的图【转载】
  7. java的maven项目(三)私服的搭建(windows版)
  8. c#读取LOG文件并解决读取提示被其他进程占用问题
  9. Jquery学习之路(三) 实现弹出层插件
  10. PowerDesigner用例图展示设置