title: How do you create a DataContext with different connection strings per user with Autofac and ASP.NET MVC?
body: <p>I am using autofac to resolve dependencies to my asp.net mvc application. Here I am creating the database factory per HTTP request like this.</p>
  
  <pre><code> builder.RegisterType&lt;DatabaseFactory&gt;().As&lt;IDatabaseFactory&gt;().WithParameter("con", SmartSession.Con).InstancePerHttpRequest();
  </code></pre>
  
  <p>Different user will pass different value for parameter "con". My purpose is providing different database connectivity to different user to my deployed application. But here my assumption gone bad. The problem is, to create new instance of databasefactory I have to run the autofac resolver whenever an user wants to login. But it also changes the datacontext for the user who are still using. So my query is how to people handle this kind scenario usually?
  I am using EF6 and Identity 2. The repositories uses a BaseRepository which uses the DatabaseFactory. DatabaseFactory returns the datacontext. </p>
  
  <pre><code> public class DatabaseFactory : Disposable, IDatabaseFactory
  {
      private SmartEntities _dataContext;
      string con;
  
      public DatabaseFactory(string con)
      {
          this.con = con;
      }
      public SmartEntities Get()
      {
          return _dataContext ?? (_dataContext = new SmartEntities(con));
      }
      protected override void DisposeCore()
      {
          if (_dataContext != null)
              _dataContext.Dispose();
      }
  }
  </code></pre>
  
  <p>In RepositoryBase</p>
  
  <pre><code>protected RepositoryBase(IDatabaseFactory databaseFactory)
      {
          DatabaseFactory = databaseFactory;
          _dbset = DataContext.Set&lt;T&gt;();
      }
  </code></pre>