title: UOW + Repository + Autofac load two different DbContext
body: <p>I'm facing an issue today and I'm unable to solve it, I searched a lot and can't get into a solution, please help me if you can.</p>
  
  <p>I'm implementing a MVC application which uses EF + Repository Pattern + Unit Of Work with Autofac as the Dependency Injector.</p>
  
  <p>I was able to work with one DbContext class, but I'm facing a situation where I need to use another DbContext instance (which access another database with another user credentials)</p>
  
  <p>Let me explain better: I have EntityA which comes from database A (and have a DatabaseA_Context class). So I need a EntityB, which comes from database B (with its own DatabaseB_Context class). </p>
  
  <p>When I register they with AutoFac, only the last configured dependency is injected on the GenericRepository implementation.</p>
  
  <p>I've already found articles saying that Autofac overrides the registration with the last value.</p>
  
  <p>I've already found other article that shows if I pass an IEnumerable on the UnitOfWork constructor, I'm able to see all of the registered types for it, but I want a specific one.</p>
  
  <p>Am I clear enough?</p>
  
  <p>My code below:</p>
  
  <p>My controller:</p>
  
  <pre><code>public class MyController : Controller
  {
      private readonly IBaseBLL&lt;EntityA&gt; _aBLL;
      private readonly IBaseBLL&lt;EntityB&gt; _bBll;
  
      public MyController(IBaseBLL&lt;EntityA&gt; aBLL, IBaseBLL&lt;EntityB&gt; bBLL)
      {
          _aBLL = aBLL;
          _bBLL = bBLL;
      }
  }
  </code></pre>
  
  <p>My Business Layer</p>
  
  <pre><code>public interface IBaseBLL&lt;T&gt; where T : class
  {
      T Select(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
      T AddT entity);
      void Update(T entity);
      T Delete(T entity);
  }
  
  public class BaseBLL&lt;T&gt; : IBaseBLL&lt;T&gt; where T : class
  {
      private readonly IUnitOfWork _uow;
  
      public BaseBLL(IUnitOfWork uow)
      {
          _uow = uow;
      }
  
      //implementation goes here...
  }
  </code></pre>
  
  <p>My UOW implementation</p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      int SaveChanges();
      IGenericRepository&lt;T&gt; Repository&lt;T&gt;() where T : class;
  }
  
  public class UnitOfWork : IUnitOfWork
  {
      private readonly DbContext _dbContext;
      private bool disposed = false;
      private Dictionary&lt;Type, object&gt; repositories;
  
  
      public UnitOfWork(DbContext dbContext)
      {
          _dbContext = dbContext;
          repositories = new Dictionary&lt;Type, object&gt;();
      }
  
      public IGenericReposity&lt;T&gt; Repository&lt;T&gt;() where T : class
      {
          if (repositories.Keys.Contains(typeof(T)))
              return repositories[typeof(T)] as IGenericReposity&lt;T&gt;;
  
          IGenericReposity&lt;T&gt; repository = new GenericRepository&lt;T&gt;(_dbContext);
          repositories.Add(typeof(T), repository );
          return repository ;
      }
  
      public int SaveChanges()
      {
          return _dbContext.SaveChanges();
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
              if (disposing)
                  _dbContext.Dispose();
  
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>
  
  <p>My Repository implementation</p>
  
  <pre><code>public class GenericRepository&lt;T&gt; : IGenericRepositoryT&gt; where T : class
  {
      protected readonly DbContext _dbContext;
      protected IDbSet&lt;T&gt; _dbSet;
  
      public GenericRepository(DbContext dbContext)
      {
          _dbContext = dbContext;
          _dbSet = _dbContext.Set&lt;T&gt;();
      }
  
      //implementation goes here...
  }
  </code></pre>
  
  <p>My AutoFac registration (in Global.asax file)</p>
  
  <pre><code>var builder = new ContainerBuilder();
  
  builder.RegisterType(typeof(DatabaseA_Context)).As(typeof(DbContext)).InstancePerLifetimeScope();
  builder.RegisterType(typeof(DatabaseB_Context)).As(typeof(DbContext)).InstancePerLifetimeScope();
  builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerRequest(); 
  </code></pre>
  
  <p>Please help</p>