title: EF Core - Using DbSet dynamically with generics
body: <p>I have an interface that is being attached in my DI</p>
  <pre><code>builder.RegisterType&lt;TaskTempo&gt;().As&lt;ITaskTempo&gt;();
  </code></pre>
  <p>This is being done because <code>ITaskTempo</code> is dynamic and could be one of multiple objects, but sharing same interface.</p>
  <p>Anyway, I have <code>DbContext</code> where I map the <code>DbSet</code> with entity manually</p>
  <pre><code>// DI register
  builder.RegisterType&lt;MapperFor890&gt;().As&lt;IEntityMapper&gt;();
  
  // ...
  // DbContext
  
  public partial class MyDBContext : DbContext
  {
      private readonly IEntityMapper _entityMapper;
  
      // Please not that I do not want to manually add all of diff implementations of ITaskTempo here
      // public virtual DbSet&lt;TaskTempo1&gt; TaskTempo { get; set; }
  
  
      public MyDBContext(DbContextOptions&lt;MyDBContext&gt; options, IEntityMapper mapper)
          : base(options)
      {
          _entityMapper = mapper;
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          // Map Entities
          _entityMapper.Map(modelBuilder, this);
      }
  }
  
  // ...
  // EntityMapper
  public class MapperFor890 : IEntityMapper
  {
      public void Map(ModelBuilder modelBuilder, DbContext context)
      {
          modelBuilder.Entity&lt;TaskTempo1&gt;(entity =&gt; 
          {
              // Map here the properties
          });
      }
  }
  </code></pre>
  <p>Then, I am able to fetch data from <code>EfRepository</code>:</p>
  <pre><code>// Global Repository
  public class EfRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
  {
      protected readonly DbContext Context;
      protected readonly ILogger&lt;EfRepository&lt;TEntity&gt;&gt; Logger;
  
      public EfRepository(DbContext context,
          ILogger&lt;EfRepository&lt;TEntity&gt;&gt; logger)
      {
          Context = context;
          Logger = logger;
      }
  
      public virtual IQueryable&lt;TEntity&gt; Find(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          Logger.LogDebug($&quot;Finding '{typeof(TEntity).Name}' with predicate&quot;);
          return Context.Set&lt;TEntity&gt;().Where(predicate);
      }
  }
  
  // TaskTempo Repository
  public class TaskTempoRepository&lt;TTaskTempo&gt; : EfRepository&lt;TTaskTempo&gt;, ITaskScanRepository&lt;TTaskTempo&gt; where TTaskTempo : class, ITaskTempo
  {
      public MyDBContext DbContext =&gt; Context as MyDBContext;
  
      public TaskScanRepository(MyDBContext context, ILogger&lt;TaskScanRepository&lt;TTaskTempo&gt;&gt; logger) : base(context,logger)
      {
      }
  }
  
  // DI injection
  builder.RegisterType&lt;TaskTempo1&gt;().As&lt;ITaskTempo&gt;();
  builder.RegisterType&lt;TaskTempoRepository&lt;ITaskTempo&gt;&gt;().As&lt;ITasTempoRepository&lt;ITaskTempo&gt;&gt;();
  </code></pre>
  <p>Once I fetch</p>
  <pre><code>_taskTempoRepository.Find(t =&gt; t.Id = 3);
  </code></pre>
  <p>I am getting the following error:</p>
  <blockquote>
  <p>Cannot create a DbSet for 'ITaskTempo' because this type is not included in the model for the context</p>
  </blockquote>