-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Entity Framework Core storage of configuration data
- Loading branch information
1 parent
16c3951
commit a09f200
Showing
9 changed files
with
1,794 additions
and
9 deletions.
There are no files selected for viewing
539 changes: 539 additions & 0 deletions
539
.../Data/Migrations/IdentityServer/Configuration/20170625145954_InitConfigration.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
501 changes: 501 additions & 0 deletions
501
IdentityApp/Data/Migrations/IdentityServer/Configuration/20170625145954_InitConfigration.cs
Large diffs are not rendered by default.
Oops, something went wrong.
538 changes: 538 additions & 0 deletions
538
...tyApp/Data/Migrations/IdentityServer/Configuration/ConfigurationDbContextModelSnapshot.cs
Large diffs are not rendered by default.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
IdentityApp/Data/Migrations/IdentityServer/IdentityServerDatabaseInitialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using IdentityServer4.EntityFramework.DbContexts; | ||
using IdentityServer4.EntityFramework.Mappers; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Linq; | ||
|
||
namespace IdentityApp.Data.Migrations.IdentityServer | ||
{ | ||
public static class IdentityServerDatabaseInitialization | ||
{ | ||
public static void InitializeDatabase(IApplicationBuilder app) | ||
{ | ||
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) | ||
{ | ||
PerformMigrations(serviceScope); | ||
SeedData(serviceScope); | ||
} | ||
} | ||
|
||
private static void PerformMigrations(IServiceScope serviceScope) | ||
{ | ||
serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>().Database.Migrate(); | ||
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate(); | ||
} | ||
|
||
private static void SeedData(IServiceScope serviceScope) | ||
{ | ||
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>(); | ||
|
||
if (!context.Clients.Any()) | ||
{ | ||
foreach (var client in Config.GetClients()) | ||
{ | ||
context.Clients.Add(client.ToEntity()); | ||
} | ||
context.SaveChanges(); | ||
} | ||
|
||
if (!context.IdentityResources.Any()) | ||
{ | ||
foreach (var resource in Config.GetIdentityResources()) | ||
{ | ||
context.IdentityResources.Add(resource.ToEntity()); | ||
} | ||
context.SaveChanges(); | ||
} | ||
|
||
if (!context.ApiResources.Any()) | ||
{ | ||
foreach (var resource in Config.GetApiResources()) | ||
{ | ||
context.ApiResources.Add(resource.ToEntity()); | ||
} | ||
context.SaveChanges(); | ||
} | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ta/Migrations/IdentityServer/PersistedGrant/20170625150103_InitPersistedGrant.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
...ityApp/Data/Migrations/IdentityServer/PersistedGrant/20170625150103_InitPersistedGrant.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace IdentityApp.Data.Migrations.IdentityServer.PersistedGrant | ||
{ | ||
public partial class InitPersistedGrant : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "PersistedGrants", | ||
columns: table => new | ||
{ | ||
Key = table.Column<string>(maxLength: 200, nullable: false), | ||
ClientId = table.Column<string>(maxLength: 200, nullable: false), | ||
CreationTime = table.Column<DateTime>(nullable: false), | ||
Data = table.Column<string>(maxLength: 50000, nullable: false), | ||
Expiration = table.Column<DateTime>(nullable: true), | ||
SubjectId = table.Column<string>(maxLength: 200, nullable: true), | ||
Type = table.Column<string>(maxLength: 50, nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_PersistedGrants", x => x.Key); | ||
}); | ||
|
||
migrationBuilder.CreateIndex( | ||
name: "IX_PersistedGrants_SubjectId_ClientId_Type", | ||
table: "PersistedGrants", | ||
columns: new[] { "SubjectId", "ClientId", "Type" }); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "PersistedGrants"); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...App/Data/Migrations/IdentityServer/PersistedGrant/PersistedGrantDbContextModelSnapshot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
using IdentityServer4.EntityFramework.DbContexts; | ||
|
||
namespace IdentityApp.Data.Migrations.IdentityServer.PersistedGrant | ||
{ | ||
[DbContext(typeof(PersistedGrantDbContext))] | ||
partial class PersistedGrantDbContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder | ||
.HasAnnotation("ProductVersion", "1.1.2") | ||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); | ||
|
||
modelBuilder.Entity("IdentityServer4.EntityFramework.Entities.PersistedGrant", b => | ||
{ | ||
b.Property<string>("Key") | ||
.HasMaxLength(200); | ||
|
||
b.Property<string>("ClientId") | ||
.IsRequired() | ||
.HasMaxLength(200); | ||
|
||
b.Property<DateTime>("CreationTime"); | ||
|
||
b.Property<string>("Data") | ||
.IsRequired() | ||
.HasMaxLength(50000); | ||
|
||
b.Property<DateTime?>("Expiration"); | ||
|
||
b.Property<string>("SubjectId") | ||
.HasMaxLength(200); | ||
|
||
b.Property<string>("Type") | ||
.IsRequired() | ||
.HasMaxLength(50); | ||
|
||
b.HasKey("Key"); | ||
|
||
b.HasIndex("SubjectId", "ClientId", "Type"); | ||
|
||
b.ToTable("PersistedGrants"); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters