title: How do I use multiple databases with Autofac?
body: <p>I am having a project and my project is connecting to two different databases are BookStoreEntities and BlogEntities. </p>
  
  <p>If I remove line code <code>builder.RegisterType&lt;BlogEntities&gt;().As&lt;DbContext&gt;();</code> in Autofac configuration my project works fine and else I'll get error "<strong>The entity type Book is not part of the model for the current context</strong>".</p>
  
  <p><strong>My autofac config</strong>:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterControllers(Assembly.GetExecutingAssembly());
  
  builder.RegisterType&lt;BookStoreEntities&gt;().As&lt;DbContext&gt;();
  builder.RegisterType&lt;BlogEntities&gt;().As&lt;DbContext&gt;();
  builder.RegisterGeneric(typeof(Repository&lt;&gt;)).As(typeof(IRepository&lt;&gt;)).InstancePerDependency();
  builder.RegisterType&lt;BookService&gt;().As&lt;IBookService&gt;();
  
  builder.RegisterFilterProvider();
  IContainer container = builder.Build();
  DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  </code></pre>
  
  <p><strong>Repository class</strong>:</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      private DbContext _dbContext;
      private DbSet&lt;T&gt; _dbSet;
  
      public Repository(DbContext dbContext)
      {
          _dbContext = dbContext;
          _dbSet = dbContext.Set&lt;T&gt;();
      }
  
      public IEnumerable&lt;T&gt; GetAll()
      {
          return _dbSet;
      }
  }
  </code></pre>
  
  <p><strong>Service layer</strong>:</p>
  
  <pre><code>public class BookService : IBookService
  {
      private IRepository&lt;Book&gt; _bookRepository;
  
      public BookService(IRepository&lt;Book&gt; bookRepository)
      {
          _bookRepository = bookRepository;
      }
  
      public IEnumerable&lt;Book&gt; GetBooks()
      {
          return _bookRepository.GetAll();
      }
  }
  </code></pre>
  
  <p><strong>Controller</strong>:</p>
  
  <pre><code>public class BookController : Controller
  {
      private IBookService _bookService; 
  
      public BookController(IBookService bookService)
      {
          _bookService = bookService;
      }
  
      // GET: Book
      public ActionResult Index()
      {
          var books = _bookService.GetBooks();
  
          return View(books);
      }
  }
  </code></pre>