解决NETCore IdentityServer4.EntityFramework 在Mysql上字段过长的问题

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 PersistedGrantDbContextConfigurationDbContext

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上运行。访问实体(或表)将是相应的上下文。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值