2017-01-29 85 views
1

當我嘗試登錄時出現錯誤: 處理請求時發生未處理的異常。SqlException:無效的對象名'OpenIddictTokens'

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

要確認的東西,這是從輸出窗口輸出:

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

我發現沒有一個單一的OpenIddict表數據庫。有很多使用OpenIddictDbContext的示例,我使用的是but there is a new approach,但有些東西不起作用。

我的啓動文件是:

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 ...)使用作爲TKey的<int>

我不知道爲什麼沒有創建OpenIdDict表。我已經將我的數據庫,創建新的和運行

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

,而只是身份表創建。

回答

1

它有助於(創建表),如果我叫modelBuilder.UseOpenIddict<int>()

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

    modelBuilder.UseOpenIddict<int>(); 

    //fix table names 
+0

我不得不跳過,因爲我用的是默認的身份模型,但你的回答讓我在那裏。 :) –