Reputation: 10459
When I try to login I get error: An unhandled exception occurred while processing the request.
SqlException: Invalid object name 'OpenIddictTokens'. System.Data.SqlClient.SqlCommand+<>c.b__107_0(Task result)
DbUpdateException: An error occurred while updating the entries. See the inner exception for details. Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+d__32.MoveNext()
To confirm things, this is output from output window:
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
I notice there is not a single OpenIddict table in database. There is a lot of examples where OpenIddictDbContext is used, but there is a new approach which I use, but something is not working.
My startup file is:
public void ConfigureServices(IServiceCollection services)
{
try
{
services.AddMvc();
services.AddEntityFrameworkSqlServer();
services.AddScoped<UserStore<AppUser, AppRole, AppDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim>, AppUserStore>();
services.AddScoped<UserManager<AppUser>, AppUserManager>();
services.AddScoped<RoleManager<AppRole>, AppRoleManager>();
services.AddScoped<SignInManager<AppUser>, AppSignInManager>();
services.AddScoped<RoleStore<AppRole, AppDbContext, int, AppUserRole, AppRoleClaim>, AppRoleStore>();
var connection = Configuration["ConnectionStrings"];
services.AddDbContext<AppDbContext>(options => {
options.UseSqlServer(connection);
options.UseOpenIddict<int>();
});
services
.AddIdentity<AppUser, AppRole>()
.AddUserStore<AppUserStore>()
.AddUserManager<AppUserManager>()
.AddRoleStore<AppRoleStore>()
.AddRoleManager<AppRoleManager>()
.AddSignInManager<AppSignInManager>()
.AddDefaultTokenProviders();
services.AddOpenIddict<int>()
.AddEntityFrameworkCoreStores<AppDbContext>()
.AddMvcBinders()
.EnableTokenEndpoint("/API/authorization/token")
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.DisableHttpsRequirement();
services.AddSingleton<DbSeeder>();
}
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());
}
}
}
and my DbContext:
public partial class AppDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//fix table names
modelBuilder.Entity<AppUser>(b =>
{
b.ToTable("Users");
});
modelBuilder.Entity<AppRole>(b =>
{
b.ToTable("Roles");
});
modelBuilder.Entity<AppUserClaim>(b =>
{
b.ToTable("UserClaims");
});
modelBuilder.Entity<AppRoleClaim>(b =>
{
b.ToTable("RoleClaims");
});
modelBuilder.Entity<AppUserRole>(b =>
{
b.ToTable("UserRoles");
});
modelBuilder.Entity<AppUserLogin>(b =>
{
b.ToTable("UserLogins");
});
modelBuilder.Entity<AppUserToken>(b =>
{
b.ToTable("UserTokens");
});
//add our
I override all my identity classes (IdentityRole, IdentityRoleClaim, IdentityUser ...) to use <int>
as TKey.
I do not know why OpenIdDict tables are not created. I already drop my database, create new and run
dotnet ef migrations add "Initial" -o "Data\Migrations"
dotnet ef database update
, but only identity tables are created.
Upvotes: 4
Views: 3935
Reputation: 9054
Do this in your Package Manager Console:
add-migration
and then
update-database
it should fix the error
Upvotes: 1
Reputation: 665
See the connection string in the "startup.cs" class and "appsettings.json" config file. In my case was the name of the instance of SQL Server Express.
Upvotes: 0
Reputation: 10459
It helps (tables are created) if I call modelBuilder.UseOpenIddict<int>()
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.UseOpenIddict<int>();
//fix table names
Upvotes: 2