title: Simple Injector with several bounded DbContexts - exception "IDbContext has already been registered"
body: <p>I am trying to switch to <strong>Simple Injector</strong> Dependency Injection framework as I am impressed with its speed.</p>
  
  <pre><code> private static void RegisterServices(Container container)
   {
       container.RegisterPerWebRequest&lt;IDbContext, DbContext1&gt;();
       ////container.RegisterPerWebRequest&lt;IDbContext, DbContext2&gt;();    
       container.RegisterPerWebRequest&lt;IUnitOfWork, UnitOfWork&gt;();
       container.RegisterPerWebRequest&lt;IColourRepository, ColourRepository&gt;();
  </code></pre>
  
  <p>where DbContext1 and DbContext2 inherit from a BaseDbContext class </p>
  
  <pre><code>public class BaseDbContext&lt;TContext&gt; : DbContext, IDbContext where TContext : DbContext
  </code></pre>
  
  <p>which implements a rather simple IDbContext interface (like the many offered on SO), e.g:</p>
  
  <pre><code>public interface IDbContext
  {
      IQueryable&lt;TEntity&gt; Find&lt;TEntity&gt;() where TEntity : class;
      DbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class;
      int SaveChanges();
      void Dispose();
  }
  </code></pre>
  
  <p>If I use just a single DbContext class, it works fine - repositories get injected, data pulled etc.</p>
  
  <p>However, I'd also like to use bounded contexts with a smaller number of DbSets in each of them (<a href="https://msdn.microsoft.com/en-us/magazine/jj883952.aspx" rel="nofollow noreferrer">Shrink EF Models with DDD Bounded Contexts</a>) as my Code-First DbContext would otherwise include hundreds of classes</p>
  
  <pre><code>private static void RegisterServices(Container container)
   {
       container.RegisterPerWebRequest&lt;IDbContext, DbContext1&gt;();
       container.RegisterPerWebRequest&lt;IDbContext, DbContext2&gt;();
  
       container.RegisterPerWebRequest&lt;IUnitOfWork, UnitOfWork&gt;();
       container.RegisterPerWebRequest&lt;IColourRepository, ColourRepository&gt;();
  </code></pre>
  
  <p>Then I get an exception:</p>
  
  <blockquote>
    <p>System.InvalidOperationException was unhandled by user code
      HResult=-2146233079
      Message=Type IDbContext has already been registered and the container is currently not configured to allow overriding registrations. To allow overriding the current registration, please set the Container.Options.AllowOverridingRegistrations to true.
      Source=SimpleInjector
      StackTrace:
           at SimpleInjector.Container.ThrowWhenTypeAlreadyRegistered(Type type)
           at SimpleInjector.Container.AddRegistration(Type serviceType, Registration registration)
           at SimpleInjector.Container.Register[TService,TImplementation](Lifestyle lifestyle, String serviceTypeParamName, String implementationTypeParamName)
           at SimpleInjector.Container.Register[TService,TImplementation](Lifestyle lifestyle)
           at SimpleInjector.SimpleInjectorWebExtensions.RegisterPerWebRequest[TService,TImplementation](Container container)</p>
  </blockquote>
  
  <p>If I follow the suggestion: </p>
  
  <pre><code>container.Options.AllowOverridingRegistrations = true;
  </code></pre>
  
  <p>then DbContext2 seems to override DbContext1, e.g. DbSet "Colour" is in DbContext1 and it is not accessible any more:</p>
  
  <pre><code>Additional information: The entity type Colour is not part of the model for the current context.
  </code></pre>
  
  <p>How should I use Simple Injector and bounded DbContexts together?</p>
  
  <p>[UPDATE]</p>
  
  <p>The DbContexts are no used in controllers directly, they are dependencies of repositories which Simple Injector should be able to initialise in constructors</p>
  
  <pre><code> public class ColoursController : ApiController
  {
      private readonly IColourRepository _repository;
      private readonly ModelFactory _modelFactory;
  
      public ColoursController(IColourRepository repository)
      {
          _repository = repository;
          _modelFactory = new ModelFactory();
      }
  </code></pre>
  
  <p>where </p>
  
  <pre><code> public class ColourRepository : Repository&lt;Colour&gt;, IColourRepository
  {
      public ColourRepository(IDbContext context) : base(context) { }
  </code></pre>
  
  <p>ColourRepository expects a concrete implementation of DbContext1, but some other repository would need DbContext2 (with a different set of entities)</p>
  
  <p>I do not see the reason why it is impossible to use IDbContext interface (or a base type) for both DbContext1 and DbContext2.</p>
  
  <p>Unity can do it:</p>
  
  <pre><code>container.RegisterType&lt;IDbContext, NorthwindContext&gt;(new PerRequestLifetimeManager(), "NorthwindContext");
  container.RegisterType&lt;IDbContext, NorthwindCustomerContext&gt;(new PerRequestLifetimeManager(), "NorthwindCustomerContext");
  </code></pre>
  
  <p>Ninject can do it.</p>
  
  <p>Simple Injector mentions CompositeLogger - maybe that one could do the trick?</p>
  
  <p><a href="https://simpleinjector.org/" rel="nofollow noreferrer">https://simpleinjector.org/</a></p>