和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)); }; })
之后执行数据库迁移命令 数据库可以正常生成了!