和Identity
的问题相似,通过替换掉默认的PersistedGrantDbContext
来实现
新建一个 PersistedGrantMysqlDbContext
类 实现默认提供的PersistedGrantDbContext
public class PersistedGrantMysqlDbContext : PersistedGrantDbContext<PersistedGrantDbContext>
{
public PersistedGrantMysqlDbContext(DbContextOptions<PersistedGrantMysqlDbContext> options, OperationalStoreOptions storeOptions) : base(options, storeOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity("IdentityServer4.EntityFramework.Entities.PersistedGrant", e => e.Property<string>("Data").HasMaxLength(20000));//这里原来是5W 超长了
}
}
在Startup
中注入我们自己实现的方法
services.AddIdentityServer()
//这里省略了其他的方法
.AddOperationalStore<PersistedGrantMysqlDbContext>(options =>//PersistedGrantMysqlDbContext替代默认方法
{ //存储Token Grants等信息
options.ConfigureDbContext = builder =>
{
//builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
builder.UseMySQL(Configuration.GetConnectionString("MysqlConnection")
, sql => sql.MigrationsAssembly(migrationAssembly));
};
})
之后执行数据库迁移命令 数据库可以正常生成了!
IdentityServer4 PersistedGrantDbContext
和ConfigurationDbContext
IdentityServer 4的新手。我在文档中的此处遵循IdentityServer4 EntityFramework示例。
运行迁移脚本后
dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
它可以工作,现在我的应用程序具有3个数据库上下文。
ApplicationDbContext
PersistedGrantDbContext
ConfigurationDbContext
我的问题是这两个数据库上下文是做什么的?应用程序数据库上下文和其他两个上下文有什么区别?
如果我更新或添加任何模型,是否需要更新所有三个模型?或者什么时候应该在ApplicationDbContext上运行迁移,什么时候应该在其他两个上运行。
答
有3个DB上下文,正如@Jasen所提到的,它是为了拆分对实体或表的访问。
IdeneityServer4 + EntityFramework + ASP.NET Identity
在数据库中创建以下表:
上下文用于引用以下内容:
ApplicationDbContext
-负责与ASP.NET Identity
相关的用户,因此表
- dbo.AspNetRoleClaims
- dbo.AspNetRoles
- dbo.AspNetUserClaims
- dbo.AspNetUserLogins
- dbo.AspNetUserRoles
- dbo.AspNetUsers
- dbo.AspNetUserTokens
PersistedGrantDbContext
-负责存储同意,授权码,刷新令牌和参考令牌
- dbo.PersistedGrants
ConfigurationDbContext
-负责数据库中剩余的所有其他内容
因此,关于迁移,如果我更新任何AspNet身份模型(即ApplicationUser
),那么我将在ApplicationDbContext
上运行迁移。任何客户端表或其他作用域将在ConfigurationDbContext
上运行。访问实体(或表)将是相应的上下文。