title: The entity type is not part of the model for the current context (autofac)
body: <p>I have an issue with EntityFramework at the moment.
  I added some new Entities to my database context, one of them was <strong>Client</strong>. Here is my <strong>DbContext</strong> with the new entries:</p>
  
  <pre><code>public class DatabaseContext : IdentityDbContext&lt;User&gt;
  {
  
      //Define our tables
      public DbSet&lt;Email&gt; Emails { get; set; }
      public DbSet&lt;Log&gt; Logs { get; set; }
      public DbSet&lt;Setting&gt; Settings { get; set; }
      public DbSet&lt;Models.Message&gt; Messages { get; set; }
      public DbSet&lt;HealthCheck&gt; HealthChecks { get; set; }
  
      public DbSet&lt;Client&gt; Clients { get; set; }
      public DbSet&lt;ClientClaim&gt; ClientClaims { get; set; }
      public DbSet&lt;ClientSecret&gt; ClientSecrets { get; set; }
      public DbSet&lt;Consent&gt; Consents { get; set; }
      public DbSet&lt;Scope&gt; Scopes { get; set; }
      public DbSet&lt;ScopeClaim&gt; ScopeClaims { get; set; }
  
      /// &lt;summary&gt;
      /// Default constructor
      /// &lt;/summary&gt;
      public DatabaseContext(CormarConfig config) : base(config.SqlConnectionString)
      {
  
          // Write our SQL to the debug window
          Database.Log = s =&gt; Debug.WriteLine(s);
  
          // Disable Lazy Loading
          base.Configuration.LazyLoadingEnabled = false;
  
          // TODO: Remove when publishing!
          Database.SetInitializer&lt;DatabaseContext&gt;(null);
      }
  
      /// &lt;summary&gt;
      /// Overrides the inherited OnModelCreated method.
      /// &lt;/summary&gt;
      /// &lt;param name="modelBuilder"&gt;The DbModelBuilder&lt;/param&gt;
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
  
          // Rename tables
          modelBuilder.Entity&lt;IdentityRole&gt;().ToTable("Roles");
          modelBuilder.Entity&lt;IdentityUserRole&gt;().ToTable("UserRoles");
          modelBuilder.Entity&lt;IdentityUserClaim&gt;().ToTable("UserClaims");
          modelBuilder.Entity&lt;IdentityUserLogin&gt;().ToTable("UserLogins");
          modelBuilder.Entity&lt;Models.Message&gt;().ToTable("Messages");
  
          // Create our foreign key
          modelBuilder.Entity&lt;IdentityRole&gt;().HasMany(m =&gt; m.Users).WithRequired().HasForeignKey(m =&gt; m.RoleId);
  
          // Create our primary keys
          modelBuilder.Entity&lt;IdentityUserLogin&gt;().HasKey&lt;string&gt;(m =&gt; m.UserId);
          modelBuilder.Entity&lt;IdentityRole&gt;().HasKey&lt;string&gt;(m =&gt; m.Id);
          modelBuilder.Entity&lt;IdentityUserRole&gt;().HasKey(m =&gt; new { m.RoleId, m.UserId });
          modelBuilder.Entity&lt;Setting&gt;().HasKey(m =&gt; new { m.Id, m.Name });
  
          //modelBuilder.Entity&lt;RefreshToken&gt;().Property(t =&gt; t.Subject).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_SubjectClient", 0) { IsUnique = true }));
          //modelBuilder.Entity&lt;RefreshToken&gt;().Property(t =&gt; t.ClientId).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_SubjectClient", 1) { IsUnique = true }));            
      }
  }
  </code></pre>
  
  <p>I have run <code>add-migration</code> and <code>update-database</code> and can see that my new entity tables have been created.
  I then created a simple class that should exposes <strong>Clients</strong>, it looks like this:</p>
  
  <pre><code>public class IdentityServerClientStore : IClientStore
  {
      private readonly DbSet&lt;Client&gt; _clients;
  
      public IdentityServerClientStore(DbContext context)
      {
          _clients = context.Set&lt;Client&gt;();
      }
  
      public async Task&lt;Client&gt; FindClientByIdAsync(string clientId) =&gt; await _clients.SingleOrDefaultAsync(m =&gt; m.ClientId.Equals(clientId));
  }
  </code></pre>
  
  <p>This is injected using <strong>Autofac</strong> and is registered like this:</p>
  
  <pre><code>builder.RegisterType&lt;DatabaseContext&gt;().As&lt;DbContext&gt;().InstancePerDependency();
  builder.RegisterType&lt;IdentityServerClientStore&gt;().As&lt;idsrv.IClientStore&gt;().InstancePerDependency();
  </code></pre>
  
  <p>But when I try to do anything, I get this error:</p>
  
  <blockquote>
    <p>"The entity type Client is not part of the model for the current context."</p>
  </blockquote>
  
  <p>The whole stack trace looks like this:</p>
  
  <pre><code>{
      "Message": "An error has occurred.",
      "ExceptionMessage": "The entity type Client is not part of the model for the current context.",
      "ExceptionType": "System.InvalidOperationException",
      "StackTrace": "   at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)\r\n   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()\r\n   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()\r\n   at System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken)\r\n   at System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate)\r\n   at Cormar.Business.Identity.IdentityServerClientStore.&lt;FindClientByIdAsync&gt;d__2.MoveNext() in C:\\Users\\JaymieJeffrey\\Documents\\GitHub\\Cormar\\Cormar.Business\\Identity\\IdentityServerClientStore.cs:line 18\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at IdentityServer3.Core.Validation.ClientSecretValidator.&lt;ValidateAsync&gt;d__2.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Validation\\ClientSecretValidator.cs:line 63\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at IdentityServer3.Core.Endpoints.TokenEndpointController.&lt;ProcessAsync&gt;d__7.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Endpoints\\Connect\\TokenEndpointController.cs:line 98\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at IdentityServer3.Core.Endpoints.TokenEndpointController.&lt;Post&gt;d__0.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Endpoints\\Connect\\TokenEndpointController.cs:line 74\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.System.Web.Http910911.TaskHelpersExtensions.&lt;CastToObject&gt;d__3`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Tracing.ITraceWriterExtensions.&lt;TraceBeginEndAsyncCore&gt;d__18`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.&lt;InvokeActionAsyncCore&gt;d__0.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Tracing.ITraceWriterExtensions.&lt;TraceBeginEndAsyncCore&gt;d__18`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.&lt;CallOnActionExecutedAsync&gt;d__5.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Web.Http.Filters.ActionFilterAttribute.&lt;CallOnActionExecutedAsync&gt;d__5.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.&lt;ExecuteActionFilterAsyncCore&gt;d__0.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.&lt;ExecuteAsync&gt;d__2.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.&lt;ExecuteAuthorizationFilterAsyncCore&gt;d__2.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Tracing.Tracers.HttpControllerTracer.&lt;ExecuteAsyncCore&gt;d__5.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Tracing.ITraceWriterExtensions.&lt;TraceBeginEndAsyncCore&gt;d__18`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0"
  }
  </code></pre>
  
  <p>Can anyone guess why this might be happening?</p>