4

当我尝试登录时出现错误:处理请求时发生未处理的异常。

SqlException:对象名称“OpenIddictTokens”无效。System.Data.SqlClient.SqlCommand+<>c.b__107_0(任务结果)

DbUpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常。Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+d__32.MoveNext()

为了确认,这是输出窗口的输出:

插入[OpenIddictTokens]([ApplicationId],[AuthorizationId],[Subject],[Type])值(@p0,@p1,@p2,@p3);选择 [Id] 从 [OpenIddictTokens] WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity(); 抛出异常:Microsoft.EntityFrameworkCore.Relational.dll 中的“System.Data.SqlClient.SqlException”

我注意到数据库中没有一个 OpenIddict 表。有很多使用 OpenIddictDbContext 的示例,但是我使用了一种新方法,但有些东西不起作用。

我的启动文件是:

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());
    }
}

}

和我的 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

我覆盖了我所有的身份类(IdentityRole、IdentityRoleClaim、IdentityUser ...)以<int>用作 TKey。

我不知道为什么没有创建 OpenIdDict 表。我已经删除了我的数据库,创建新的并运行

dotnet ef migrations add "Initial" -o "Data\Migrations"
dotnet ef database update

,但只创建身份表。

4

3 回答 3

2

如果我调用它会有所帮助(创建表)modelBuilder.UseOpenIddict<int>()

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.UseOpenIddict<int>();

    //fix table names
于 2017-01-30T12:18:26.153 回答
1

在您的包管理器控制台中执行此操作:

add-migration

进而

update-database

它应该修复错误

于 2019-05-15T05:47:44.263 回答
0

请参阅“startup.cs”类和“appsettings.json”配置文件中的连接字符串。在我的例子中是 SQL Server Express 实例的名称。

于 2018-06-01T22:14:10.857 回答