title: Using multiple DbContexts with a generic repository and unit of work
body: <p>My application is getting larger and so far I have a single <code>MyDbContext</code> which has all the tables I need in my application. I wish (for the sake of overview) to split them up into multiple <code>DbContext</code>, like <code>MainDbContext</code>, <code>EstateModuleDbContext</code>, <code>AnotherModuleDbContext</code> and <code>UserDbContext</code>.</p>
  
  <p>I am unsure how this is done probably as I am right now using dependecy injection (ninject) to place my DbContext on my UnitOfWork class like:</p>
  
  <pre><code>kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork&lt;MyDbContext&gt;));
  </code></pre>
  
  <p>Should I drop this approach with dependency injection and explicit set the <code>DbContext</code> I wish to use on my services like:</p>
  
  <pre><code>private readonly EstateService _estateService;
  
  public HomeController()
  {
      IUnitOfWork uow = new UnitOfWork&lt;MyDbContext&gt;();
      _estateService = new EstateService(uow);
  }
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>private readonly EstateService _estateService;
  
  public HomeController(IUnitOfWork uow)
  {
      _estateService = new EstateService(uow);
  }
  </code></pre>
  
  <p>Or this there another better approach? Also as a side question, I dont like passing the <code>uow</code> to my service - is there another (better) approach?</p>
  
  <p><strong>Code</strong></p>
  
  <p><strong>I have this IDbContext and MyDbContext:</strong></p>
  
  <pre><code>public interface IDbContext
  {
      DbSet&lt;T&gt; Set&lt;T&gt;() where T : class;
  
      DbEntityEntry&lt;T&gt; Entry&lt;T&gt;(T entity) where T : class;
  
      int SaveChanges();
  
      void Dispose();
  }
  
  public class MyDbContext : DbContext, IDbContext
  {
      public DbSet&lt;Table1&gt; Table1 { get; set; }
      public DbSet&lt;Table2&gt; Table1 { get; set; }
      public DbSet&lt;Table3&gt; Table1 { get; set; }
      public DbSet&lt;Table4&gt; Table1 { get; set; }
      public DbSet&lt;Table5&gt; Table1 { get; set; }
      /* and so on */
  
      static MyDbContext()
      {
          Database.SetInitializer&lt;MyDbContext&gt;(new CreateDatabaseIfNotExists&lt;MyDbContext&gt;());
      }
  
      public MyDbContext()
          : base("MyDbContext")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
  
      }
  }
  </code></pre>
  
  <p><strong>Then I have this IRepository and the implementation:</strong></p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T : class
  {
      IQueryable&lt;T&gt; GetAll();
  
      void Add(T entity);
  
      void Delete(T entity);
  
      void DeleteAll(IEnumerable&lt;T&gt; entity);
  
      void Update(T entity);
  
      bool Any();
  }
  
  public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      private readonly IDbContext _context;
      private readonly IDbSet&lt;T&gt; _dbset;
  
      public Repository(IDbContext context)
      {
          _context = context;
          _dbset = context.Set&lt;T&gt;();
      }
  
      public virtual IQueryable&lt;T&gt; GetAll()
      {
          return _dbset;
      }
  
      public virtual void Add(T entity)
      {
          _dbset.Add(entity);
      }
  
      public virtual void Delete(T entity)
      {
          var entry = _context.Entry(entity);
          entry.State = EntityState.Deleted;
          _dbset.Remove(entity);
      }
  
      public virtual void DeleteAll(IEnumerable&lt;T&gt; entity)
      {
          foreach (var ent in entity)
          {
              var entry = _context.Entry(ent);
              entry.State = EntityState.Deleted;
              _dbset.Remove(ent);
          }
      }
  
      public virtual void Update(T entity)
      {
          var entry = _context.Entry(entity);
          _dbset.Attach(entity);
          entry.State = EntityState.Modified;
      }
  
      public virtual bool Any()
      {
          return _dbset.Any();
      }
  }
  </code></pre>
  
  <p><strong>And the IUnitOfWork and implemention which handles the work done with the DbContext</strong></p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      IRepository&lt;TEntity&gt; GetRepository&lt;TEntity&gt;() where TEntity : class;
  
      void Save();
  }
  
  public class UnitOfWork&lt;TContext&gt; : IUnitOfWork where TContext : IDbContext, new()
  {
      private readonly IDbContext _ctx;
      private readonly Dictionary&lt;Type, object&gt; _repositories;
      private bool _disposed;
  
      public UnitOfWork()
      {
          _ctx = new TContext();
          _repositories = new Dictionary&lt;Type, object&gt;();
          _disposed = false;
      }
  
      public IRepository&lt;TEntity&gt; GetRepository&lt;TEntity&gt;() where TEntity : class
      {
          // Checks if the Dictionary Key contains the Model class
          if (_repositories.Keys.Contains(typeof(TEntity)))
          {
              // Return the repository for that Model class
              return _repositories[typeof(TEntity)] as IRepository&lt;TEntity&gt;;
          }
  
          // If the repository for that Model class doesn't exist, create it
          var repository = new Repository&lt;TEntity&gt;(_ctx);
  
          // Add it to the dictionary
          _repositories.Add(typeof(TEntity), repository);
  
          return repository;
      }
  
      public void Save()
      {
          _ctx.SaveChanges();
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (this._disposed) return;
  
          if (disposing)
          {
              _ctx.Dispose();
          }
  
          this._disposed = true;
      }
  } 
  </code></pre>