Changing schema on IdentityServer when customizing the ConfigurationDbContext

asp.net-core ef-migrations entity-framework-core identityserver4

Question

Having a custom class of Client as IdSrvClient and implementing the following interface for Mapping it to the database

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);
        }
    }

with this DbContext, Ef is not able to generate the Schema name that I am explicitly passing

  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());
        }
    }

It is adding the extra columns I want for my tables but is not changing the Schema. What is wrong here?

1
2
5/24/2018 12:11:36 AM

Popular Answer

I created my own DesignTimeDbContextFactoryBase and there you can set the DefaultSchema, I tried adding that on the OnConfiguring as I posted in the question but I couldn't make it work on that way

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
5/24/2018 9:27:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow