在自定义ConfigurationDbContext时更改IdentityServer上的模式

时间:2018-05-24 00:11:36

标签: asp.net-core entity-framework-core identityserver4 ef-migrations

将自定义类Client作为IdSrvClient并实现以下接口以将其映射到数据库

public class IdSrvClient : Client, IEntityBase
{
    //adding my extra properties
}

 public class IdSrvClientMap : IEntityTypeConfiguration<IdSrvClient>
    {
        public void Configure(EntityTypeBuilder<IdSrvClient> builder)
        {
            builder.ToTable("Client", "Security");
            //builder.HasQueryFilter(app => !app.IsDeleted);

            builder.Property(x => x.Id).ValueGeneratedOnAdd();

            builder.Property(x => x.ClientId).HasMaxLength(200).IsRequired();
            builder.Property(x => x.ProtocolType).HasMaxLength(200).IsRequired();
            builder.Property(x => x.ClientName).HasMaxLength(200);
            builder.Property(x => x.ClientUri).HasMaxLength(2000);
            builder.Property(x => x.LogoUri).HasMaxLength(2000);
            builder.Property(x => x.Description).HasMaxLength(1000);
            builder.Property(x => x.FrontChannelLogoutUri).HasMaxLength(2000);
            builder.Property(x => x.BackChannelLogoutUri).HasMaxLength(2000);
            builder.Property(x => x.ClientClaimsPrefix).HasMaxLength(200);
            builder.Property(x => x.PairWiseSubjectSalt).HasMaxLength(200);

            builder.HasIndex(x => x.ClientId).IsUnique();

            builder.HasMany(x => x.AllowedGrantTypes).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.RedirectUris).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.PostLogoutRedirectUris).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.AllowedScopes).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.ClientSecrets).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.Claims).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.IdentityProviderRestrictions).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.AllowedCorsOrigins).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(x => x.Properties).WithOne(x => x.Client).IsRequired().OnDelete(DeleteBehavior.Cascade);
        }
    }

使用此DbContext,Ef无法生成我明确传递的模式名称

  public class IdSrvConfigurationDbContext : ConfigurationDbContext
    {
        public IdSrvConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions) : base(options, storeOptions)
        {
        }

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

            modelBuilder.ApplyConfiguration(new IdSrvClientMap());
        }
    }

它正在为我的表添加我想要的额外列,但不会更改Schema。这有什么不对?

2 个答案:

答案 0 :(得分:1)

我创建了自己的DesignTimeDbContextFactoryBase,你可以在那里设置DefaultSchema,我尝试在OnConfiguring上添加它,因为我在问题中发布了但是我不能让它按照这种方式工作

public class IdSrvDbContextFactory : DesignTimeDbContextFactoryBase<IdSrvConfigurationDbContext>
{
    #region Methods

    protected override IdSrvConfigurationDbContext CreateNewInstance(DbContextOptions<IdSrvConfigurationDbContext> options)
    {
        var confOptions = new ConfigurationStoreOptions
        {
            DefaultSchema = "Security"
        };

        return new IdSrvConfigurationDbContext(options, confOptions);
    }

    #endregion
}

答案 1 :(得分:0)

如果您使用的是EF核心,请指定默认架构

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("MyDefaultDbSchema");
    }

同样在IdentityServer4中,大多数情况下您可以在不扩展基本客户端的情况下逃脱。例如,使用ClientProperty表来保存所需的数据。否则,我建议使用引用clientId的附加表创建新模式。