0

I have multiple databases and I need to connect each database when the user selects from dropdown list. I need to create a generic DBContext which connects to multiple database. I started with this but not going anywhere. This is already existing example but I did not understand what is BaseEntity. I took the help from following link:

Connect multiple Databases to .NET core project via Entity Framework Core

public class GenericRepository<TContext, TEntity> : IGenericRepository<TContext, TEntity> 
where TContext : DbContext
where TEntity : BaseEntity
    {

        private readonly TContext _context;
        private DbSet<T> entities;


        public GenericRepository(TContext context)
        {
            _context = context;
            entities = context.Set<T>();
        }
       public List<T> GetAll()
        {

            return entities.AsNoTracking().ToList();
        }
    }
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • 2
    While you could, there's not much point wrapping a repository over a `DBContext` instance. – Tanveer Badar Dec 22 '20 at 16:55
  • An `Entity` lives in a single database. Right? So create Repositories for each entity ( so a repo uses context-x, another repo uses context-y etc..), then create a service and simply inject / use each of these Repositories. – Ergis Dec 22 '20 at 17:05
  • If you don't have a `BaseEntity` it should probably just be `where TEntity : class`. – Gert Arnold Dec 22 '20 at 20:05

0 Answers0