title: How to identify a particular entity's Session Factory with Fluent NHibernate and Multiple Databases
body: <p>Question follows on from <a href="https://stackoverflow.com/questions/2655861/fluent-nhibernate-multiple-databases">Fluent NHibernate + multiple databases</a> (no need to follow this link,there should be enough background here).</p>
  
  <p>My problem is this:</p>
  
  <p>I'm using Fluent NHibernate. My application uses multiple databases. Each database has its own entities registered (mapped) against it. The result is that have multiple Session Factories, each one relating to a single DB, and each 'containing' its own set of mapped entities.</p>
  
  <p>For loading entities I've created a generic Factory class that provides some standard load methods usable for any registered entity (in any DB). The problem is: The load methods need to use the correct session factory for the entity class I'm busy dealing with. How would I determine which session factory I need to use? I have all the Session Factories 'on hand' (and indexed by database name), I just need a way, knowing just the type of Entity I'm about to load, of choosing the right Session Factory to use.</p>
  
  <p>For example:</p>
  
  <pre><code>public IBaseBusinessObject CreatePopulatedInstance(Type boType, Guid instanceKey)
  {
    IBaseBusinessObject result = null;
    ISessionFactory sessionFactory = GetSessionFactory(boType);
    using (ISession session = sessionFactory.OpenSession())
    {
      using (session.BeginTransaction())
      {
        result = (IBaseBusinessObject)session.Get(boType, instanceKey);
      }
    }
    return result;
  }
  </code></pre>
  
  <p>What needs to go on in GetSessionFactory(boType) ?</p>
  
  <p>Thanks for reading!</p>