首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >SqlException:无效的对象名称'OpenIddictTokens‘

SqlException:无效的对象名称'OpenIddictTokens‘
EN

Stack Overflow用户
提问于 2017-01-29 18:20:50
回答 3查看 2.6K关注 0票数 4

当我尝试登录时,我得到错误:在处理请求时发生未处理的异常。

OpenIddictTokens:无效的对象名称‘

SqlException’。System.Data.SqlClient.SqlCommand+<>c.b__107_0(Task结果)

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

为了确认一些事情,这是从输出窗口中输出:

插入ROWCOUNT值(@p0,@p1,@p2,@p3);从OpenIddictTokens中选择Id WHERE @@ROWCOUNT =1

Id = scope_identity();抛出异常: Microsoft.EntityFrameworkCore.Relational.dll中的'System.Data.SqlClient.SqlException‘

我注意到数据库中没有一个OpenIddict表。有很多使用OpenIddictDbContext的例子,比如我使用的but there is a new approach,但有些东西不起作用。

我的启动文件是:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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表。我已经删除了我的数据库,创建了新的并运行了

代码语言:javascript
复制
dotnet ef migrations add "Initial" -o "Data\Migrations"
dotnet ef database update

,但仅创建标识表。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-30 20:18:26

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

代码语言:javascript
复制
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.UseOpenIddict<int>();

    //fix table names
票数 2
EN

Stack Overflow用户

发布于 2019-05-15 13:47:44

在您的Package Manager控制台中执行此操作:

代码语言:javascript
复制
add-migration

然后

代码语言:javascript
复制
update-database

它应该可以修复这个错误

票数 1
EN

Stack Overflow用户

发布于 2018-06-02 06:14:11

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41919797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档