title: SqlException: Invalid object name 'OpenIddictTokens'
body: <p>When I try to login I get error:
  An unhandled exception occurred while processing the request.</p>
  
  <blockquote>
    <p>SqlException: Invalid object name 'OpenIddictTokens'.
    System.Data.SqlClient.SqlCommand+&lt;>c.b__107_0(Task
    result)</p>
    
    <p>DbUpdateException: An error occurred while updating the entries. See
    the inner exception for details.
    Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+d__32.MoveNext()</p>
  </blockquote>
  
  <p>To confirm things, this is output from output window:</p>
  
  <blockquote>
    <p>INSERT INTO [OpenIddictTokens] ([ApplicationId], [AuthorizationId],
    [Subject], [Type]) VALUES (@p0, @p1, @p2, @p3); SELECT [Id] FROM
    [OpenIddictTokens] WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
    Exception thrown: 'System.Data.SqlClient.SqlException' in
    Microsoft.EntityFrameworkCore.Relational.dll</p>
  </blockquote>
  
  <p>I notice there is not a single OpenIddict table in database. There is a lot of examples where OpenIddictDbContext is used, <a href="https://stackoverflow.com/questions/41122686/the-type-or-namespace-name-openiddictdbcontext-could-not-be-found">but there is a new approach</a> which I use, but something is not working.</p>
  
  <p>My startup file is:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      try
      {
          services.AddMvc();
  
          services.AddEntityFrameworkSqlServer();
  
          services.AddScoped&lt;UserStore&lt;AppUser, AppRole, AppDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim&gt;, AppUserStore&gt;();
          services.AddScoped&lt;UserManager&lt;AppUser&gt;, AppUserManager&gt;();
          services.AddScoped&lt;RoleManager&lt;AppRole&gt;, AppRoleManager&gt;();
          services.AddScoped&lt;SignInManager&lt;AppUser&gt;, AppSignInManager&gt;();
          services.AddScoped&lt;RoleStore&lt;AppRole, AppDbContext, int, AppUserRole, AppRoleClaim&gt;, AppRoleStore&gt;();
  
          var connection = Configuration["ConnectionStrings"];
          services.AddDbContext&lt;AppDbContext&gt;(options =&gt; {
              options.UseSqlServer(connection);
              options.UseOpenIddict&lt;int&gt;();
          });
  
          services
              .AddIdentity&lt;AppUser, AppRole&gt;()
              .AddUserStore&lt;AppUserStore&gt;()
              .AddUserManager&lt;AppUserManager&gt;()
              .AddRoleStore&lt;AppRoleStore&gt;()
              .AddRoleManager&lt;AppRoleManager&gt;()
              .AddSignInManager&lt;AppSignInManager&gt;()
              .AddDefaultTokenProviders();
  
          services.AddOpenIddict&lt;int&gt;()
              .AddEntityFrameworkCoreStores&lt;AppDbContext&gt;()
              .AddMvcBinders()
              .EnableTokenEndpoint("/API/authorization/token")
              .AllowPasswordFlow()
              .AllowRefreshTokenFlow()
              .DisableHttpsRequirement();
  
          services.AddSingleton&lt;DbSeeder&gt;();
  
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.ToString());
          throw;
      }
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, DbSeeder dbSeeder)
  {
      loggerFactory.AddConsole(Configuration.GetSection("Logging"));
      loggerFactory.AddDebug();
  
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
          app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
          {
              HotModuleReplacement = true
          });
      }
      else
      {
  
      }
  
      app.UseStaticFiles();
  
      app.UseIdentity();
  
      app.UseOpenIddict();
  
      app.UseOAuthValidation();
  
      app.UseMvc();
  
      try
      {
  
          dbSeeder.SeedAsync();
      }
      catch (AggregateException e)
      {
          throw new Exception(e.ToString());
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and my DbContext:</p>
  
  <pre><code>public partial class AppDbContext : IdentityDbContext&lt;AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken&gt;
  {
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          //fix table names
          modelBuilder.Entity&lt;AppUser&gt;(b =&gt;
          {
              b.ToTable("Users");
          });
  
          modelBuilder.Entity&lt;AppRole&gt;(b =&gt;
          {
              b.ToTable("Roles");
          });
  
          modelBuilder.Entity&lt;AppUserClaim&gt;(b =&gt;
          {
              b.ToTable("UserClaims");
          });
  
          modelBuilder.Entity&lt;AppRoleClaim&gt;(b =&gt;
          {
              b.ToTable("RoleClaims");
          });
  
          modelBuilder.Entity&lt;AppUserRole&gt;(b =&gt;
          {
              b.ToTable("UserRoles");
          });
  
          modelBuilder.Entity&lt;AppUserLogin&gt;(b =&gt;
          {
              b.ToTable("UserLogins");
          });
  
          modelBuilder.Entity&lt;AppUserToken&gt;(b =&gt;
          {
              b.ToTable("UserTokens");
          });
  
  
          //add our
  </code></pre>
  
  <p>I override all my identity classes (IdentityRole, IdentityRoleClaim, IdentityUser ...) to use <code>&lt;int&gt;</code> as TKey.</p>
  
  <p><strong>I do not know why OpenIdDict tables are not created. I already drop my database, create new and run</strong></p>
  
  <pre><code>dotnet ef migrations add "Initial" -o "Data\Migrations"
  dotnet ef database update
  </code></pre>
  
  <p><strong>, but only identity tables are created.</strong></p>