title: How to use Generic Repository, Unit of Work pattern with multiple EDMX for Multiple Databases
body: <p>I'm on the development of a project which has 3 MS SQL Server databases.</p>
  
  <p>I use,</p>
  
  <ul>
  <li>EntityFramework 6.4.0,</li>
  <li>Generic Repository,</li>
  <li>Repository Unit of Work Pattern.</li>
  <li>ASP.Net MVC</li>
  <li>Multipe Entity Data Models</li>
  </ul>
  
  <p>I need to save changes into multiple tables in multiple DBs as one transaction (Using one <code>SaveChanges()</code> method)</p>
  
  <p>I have done projects using the above patterns using a single database, single UoW class, single generic repository. Now I'm stuck with multiple DBs.</p>
  
  <p>My generic repository Class as follows</p>
  
  <pre><code>    public class GenericRepository&lt;TEntity, TContext&gt; : IGenericRepository&lt;TEntity, TContext&gt; 
          where TEntity : class 
          where TContext : DbContext
      {
          internal DbContext context;
          internal DbSet&lt;TEntity&gt; dbSet;
  
          public GenericRepository(DbContext context)
          {
              this.context = context;
              this.dbSet = context.Set&lt;TEntity&gt;();
          }
  
  
          public IEnumerable&lt;TEntity&gt; GetAll()
          {
              return context.Set&lt;TEntity&gt;().ToList();
          }
  
  
          public virtual IEnumerable&lt;TEntity&gt; Get(
              Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null,
              Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null,
              string includeProperties = "")
          {
              IQueryable&lt;TEntity&gt; query = dbSet;
  
              if (filter != null)
              {
                  query = query.Where(filter);
              }
  
              foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
              {
                  query = query.Include(includeProperty);
              }
  
              if (orderBy != null)
              {
                  return orderBy(query).ToList();
              }
              else
              {
                  return query.ToList();
              }
          }
  
          public virtual TEntity GetByID(object id)
          {
              return dbSet.Find(id);
          }
  
          public virtual void Insert(TEntity entity)
          {
              dbSet.Add(entity);
          }
  
          public virtual void InsertRange(IList&lt;TEntity&gt; entityList)
          {
              dbSet.AddRange(entityList);
          }
  
          public virtual void Delete(object id)
          {
              TEntity entityToDelete = dbSet.Find(id);
              Delete(entityToDelete);
          }
  
          public virtual void DeleteRange(IEnumerable&lt;TEntity&gt; lstEntity)
          {
               dbSet.RemoveRange(lstEntity);
          }
  
          public virtual void Delete(TEntity entityToDelete)
          {
              if (context.Entry(entityToDelete).State == EntityState.Detached)
              {
                  dbSet.Attach(entityToDelete);
              }
              dbSet.Remove(entityToDelete);
          }
  
          public virtual void Update(TEntity entityToUpdate)
          {
              dbSet.Attach(entityToUpdate);
              context.Entry(entityToUpdate).State = EntityState.Modified;
          }
      }
  </code></pre>
  
  <p>My Unit Of Work class as follows;</p>
  
  <pre><code>    public class UnitOfWork&lt;TContext&gt; : IDisposable where TContext : DbContext, new()
      {
          private DbContext context;
  
          private IGenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_SIN, DbContext&gt; tbl_Data_EmployeeData_sin;
          public IGenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_SIN, DbContext&gt; Tbl_Data_EmployeeData_sin
          {
              get
              {
                  if (this.tbl_Data_EmployeeData_sin == null)
                  {
                      this.tbl_Data_EmployeeData_sin = new GenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_SIN, DbContext&gt;(context);
                  }
                  return tbl_Data_EmployeeData_sin;
              }
          }
  
          private IGenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_PHL, DbContext&gt; tbl_Data_EmployeeData_phl;
          public IGenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_PHL, DbContext&gt; Tbl_Data_EmployeeData_phl
          {
              get
              {
                  if (this.tbl_Data_EmployeeData_phl == null)
                  {
                      this.tbl_Data_EmployeeData_phl = new GenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_PHL, DbContext&gt;(context);
                  }
                  return tbl_Data_EmployeeData_phl;
              }
          }
  
          private IGenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_ENG, DbContext&gt; tbl_Data_EmployeeData_eng;
          public IGenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_ENG, DbContext&gt; Tbl_Data_EmployeeData_eng
          {
              get
              {
                  if (this.tbl_Data_EmployeeData_eng == null)
                  {
                      this.tbl_Data_EmployeeData_eng = new GenericRepository&lt;TBL_RC_DATA_PHL_EmployeeData_ENG, DbContext&gt;(context);
                  }
                  return tbl_Data_EmployeeData_eng;
              }
          }
  
          public UnitOfWork()
          {
              context = new TContext();
          }
  
          public void Save()
          {
              context.SaveChanges();
          }
  
          private bool disposed = false;
          protected virtual void Dispose(bool disposing)
          {
              if (!this.disposed)
              {
                  if (disposing)
                  {
                      context.Dispose();
                  }
              }
              this.disposed = true;
          }
  
          public void Dispose()
          {
              Dispose(true);
              GC.SuppressFinalize(this);
          }
      }
  </code></pre>
  
  <p>My service layer classes as follows</p>
  
  <pre><code>  public class svcEmployeeData
      {
          private UnitOfWork&lt;DAL.RCPlusEntities&gt; unitOfWork;
  
          public svcEmployeeData()
          {
              unitOfWork = new UnitOfWork&lt;DAL.RCPlusEntities&gt;();
          }
  
          public void updateEmployees(TBL_RC_DATA_PHL_EmployeeData_SIN sin, TBL_RC_DATA_PHL_EmployeeData_ENG eng)
          {
          //updating Business Logic goes here
  
          unitOfWork.Tbl_Data_EmployeeData_sin.Update(sin);        
          unitOfWork.Tbl_Data_EmployeeData_eng.Update(eng);
          unitOfWork.Save();
  
          }
  }
  </code></pre>
  
  <p>How to transform this architecture into 3 EDMXes by 3 databases...?</p>
  
  <p>Thanks in advance.</p>