I have identityServer4 as my oAuth server in my net core appilication. I was able to initialize my client database using the following code:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
SeedData.EnsureSeedData(serviceScope);
}
}
}
The migration and client initialization is done using
public class SeedData
{
public static void EnsureSeedData(IServiceScope serviceScope)
{
Console.WriteLine("Seeding database...");
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
context.Database.Migrate();
EnsureSeedData(context);
Console.WriteLine("Done seeding database.");
Console.WriteLine();
}
private static void EnsureSeedData(ConfigurationDbContext context)
{
if (!context.Clients.Any())
{
Console.WriteLine("Clients being populated");
foreach (var client in Config.GetClients().ToList())
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("Clients already populated, update clients");
foreach (var client in Config.GetClients().ToList())
{
var item = context.Clients.Where(c => c.ClientId == client.ClientId).FirstOrDefault();
if(item == null)
{
context.Clients.Add(client.ToEntity());
} else {
var model = client.ToEntity();
model.Id = item.Id;
context.Entry(item).CurrentValues.SetValues(model);
}
}
context.SaveChanges();
}
if (!context.IdentityResources.Any())
{
Console.WriteLine("IdentityResources being populated");
foreach (var resource in Config.GetIdentityResources().ToList())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("IdentityResources already populated");
foreach (var resource in Config.GetIdentityResources().ToList())
{
var item = context.IdentityResources.Where(c => c.Name == resource.Name).FirstOrDefault();
if (item == null)
{
context.IdentityResources.Add(resource.ToEntity());
}
else
{
var model = resource.ToEntity();
model.Id = item.Id;
context.Entry(item).CurrentValues.SetValues(model);
}
}
context.SaveChanges();
}
if (!context.ApiResources.Any())
{
Console.WriteLine("ApiResources being populated");
foreach (var resource in Config.GetApiResources().ToList())
{
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("ApiResources already populated");
foreach (var resource in Config.GetApiResources().ToList())
{
var item = context.ApiResources.Where(c => c.Name == resource.Name).FirstOrDefault();
if (item == null)
{
context.ApiResources.Add(resource.ToEntity());
}
else
{
var model = resource.ToEntity();
model.Id = item.Id;
context.Entry(item).CurrentValues.SetValues(model);
}
}
context.SaveChanges();
}
}
}
The initialization of the client database works well. However, there are problems to update the client database. Some of the records are not updated. Is there a better way to do this?