IdentitityServer persisted to Database
Hi,
Does someone managed with IdentityServer persisted to database ? Not InMemory Configuration.
I followed the guidelines, but i have an error when I try to add migrations: dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Migrations/IdentityServer/PersistedGrantDbDb
Error: No DbContext named 'PersistedGrantDbContext' was found.
Or
No DbContext named 'ConfigurationDbContext' was found if i add dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Migrations/IdentityServer/ConfigurationDb
Any help is appreciated.
Thanks.
Hi @mrccarvalho ,
did you implement the two necessary DbContexts & DbContextOptionsBuilders, which read the respective configuration and instantiate the DbContexts?
Best regards,
Florian
Hi @fhebel ,
first of all, thank you so much for your reply.
I have this class and now the Contexts were found.
example for ConfigurationDbContext :
public class ConfigurationDbContext : IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext<ConfigurationDbContext>
{ public ConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions) : base(options, storeOptions) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { base.OnConfiguring(optionsBuilder); } } }
on the other hand, i've got this error (after verbose migrations):
Found DbContext 'ConfigurationDbContext'. Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ConfigurationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---> System.MissingMethodException: No parameterless constructor defined for type 'Clinguas.WebHost.Data.ConfigurationDbContext'.
Thank, Best regards!
Hi @mrccarvalho,
sorry for my late answer.
Unfortunately it's more complicated. Keep in mind that dotnet ef migrations will actually run your code and connect to the database ...
Ok, so a good place to start is to create two context factories (sample: MigrationSimplDbContextFactory.cs):
public class ConfigurationContextFactory : IDesignTimeDbContextFactory<ConfigurationDbContext>
public class PersistedGrantContextFactory : IDesignTimeDbContextFactory<PersistedGrantDbContext>
Then you have to add your two new DbContexts to Startup.cs:
services.AddIdentityServerDataStore(_configuration);
I would suggest to implement
public static IServiceCollection AddIdentityServerDataStore(this IServiceCollection services, IConfiguration configuration)
in ServiceCollectionExtensions.cs like
public static IServiceCollection AddCustomizedDataStore(this IServiceCollection services, IConfiguration configuration)
And finally you need to initialise your IdentityServerDb in Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
I hope that helps ...
Best regards,
Florian
PS: You might want to go for a solution like I did for my commercial customers - that is to separate the tables of SimplCommerce from those in IdentityServer. Given that I'm using postgres I created a new schema and put the IdentityServer related tables there ...