和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中注入我们自己实现的方法
1 services.AddIdentityServer() 2 //这里省略了其他的方法 3 .AddOperationalStore<PersistedGrantMysqlDbContext>(options =>//PersistedGrantMysqlDbContext替代默认方法 4 {//存储Token Grants等信息 5 options.ConfigureDbContext = builder => 6 { 7 //builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection") 8 builder.UseMySQL(Configuration.GetConnectionString("MysqlConnection") 9 , sql => sql.MigrationsAssembly(migrationAssembly)); 10 }; 11 })
之后执行数据库迁移命令 数据库可以正常生成了!