title: Fluent NHibernate with ASP.NET MVC 2 - Session management with 1 connection string per user
body: <p>I need to configure my Fluent NHibernate / ASP.NET MVC 2 application to use a single connection/connection string for each user.  This is because:</p>
  
  <ol>
  <li>There are multiple databases, all with the same schema, that the user can choose to log into.</li>
  <li>The legacy application, with which this ASP.NET app works in tandem, is based on the DB vendor's platform and therefore insists on using DB user accounts.</li>
  <li>Trying to convince the DBA to provide a single do-everything user account would be even more painful and time consuming than just figuring out how to make it work.</li>
  </ol>
  
  <p>I feel I have to explain my reasons in order to avoid several "Don't do that" answers.</p>
  
  <p>So anyway, I first set everything up the normal, correct way with a single connection string.  I used an LLBLGen-created <code>SessionManager</code> to create a <code>SessionFactory</code>.  The <code>SessionFactory</code> gets created and stored during <code>Application_Start()</code>.  I also have an <code>IHttpModule</code> that binds the NHibernate session to the current session context.  (I forget whose website I got that from).</p>
  
  <p>What I now <strong>want</strong> to do is have the users arrive at a login page.  They will choose their database and enter a username and password.  The application will create a database connection, store it in the user's session, and NHibernate will pick up that database connection and use it.</p>
  
  <p>Any tips about doing this in general would be very helpful, but really I need to just get over the initial hurdles:  If I remove the DB connection info from the <code>SessionFactory</code> configuration, it complains and throws an exception.  If I don't remove the DB connection info from the SessionFactory configuration, it seems to use that connection string even when provided a different connection using <code>OpenSession(IDbConnection)</code>.  Also, now that I don't even want NHibernate to do anything until the user has logged in, I feel like my <code>IHttpModule</code> may be causing some of these problems.</p>
  
  <p>Apologies if this sounds more like an incoherent rant than a question; I've just been banging my head against the wall for a while now.  Any and all suggestions on best practices (other than "Use one connection string, idiot!") are appreciated.</p>