title: Best Practice of Repository and Unit of Work Pattern with Multiple DbContext
body: <p>I plan to develop a web application using ASP.NET MVC with Entity Framework 6 (Code First / POCO). I also want to use generic Repository and Unit of Work Pattern in my application. This application connects to more than two databases so, I have to use multiple DbContext in the application. </p>
  
  <pre><code>public class ContextOne : DbContext
  {
      public DbSet&lt;Model_One1&gt;
      public DbSet&lt;Model_One2&gt;
  }
  
  public class ContextTwo : DbContext
  {
      public DbSet&lt;Model_Two1&gt;
      public DbSet&lt;Model_Two2&gt;
  }
  
  public class ContextThree : DbContext
  {
      public DbSet&lt;Model_Three1&gt;
      public DbSet&lt;Model_Three2&gt;
  }
  
  public interface IRepository&lt;T&gt; where T : DbContext
  {
      void Add&lt;T&gt;(T entity) where T : class;
  }
  
  public class Repository&lt;T&gt; where T : DbContext
  {
      void Add&lt;T&gt;(T entity) where T : class
      {
          //T is DbContext and Model. So confusing
      }
  }
  
  public interface IUnitOfWork&lt;IRepository&gt;
  {
  }
  
  public class UnitOfWork&lt;IRepository&gt;
  {
      //IRepository contains more than one DbContext how can I initiate them here?
  }
  
  //in application should look like this
  public class BaseController : Controller
  {
      protected IRepository repository = new .. //here I have no idea with multiple DbContext
  }
  
  public class HomeController : BaseController
  {
      public ActionResult Add(Model_Two2 model)
      {
          base.repository.Add&lt;Model_Two2&gt;(model)
      }
  }
  </code></pre>
  
  <p>If I call the IRepository and IUnitOfWork from Controller how can I know the matching context? What's the best practice of this problem?</p>