109 lines
3.5 KiB
C#
109 lines
3.5 KiB
C#
|
using System;
|
||
|
using System.Threading.Tasks;
|
||
|
using BlueWest.Data;
|
||
|
using BlueWest.WebApi.EF;
|
||
|
using BlueWest.WebApi.EF.Model;
|
||
|
using Duende.IdentityServer.EntityFramework.Entities;
|
||
|
using Duende.IdentityServer.EntityFramework.Interfaces;
|
||
|
using Duende.IdentityServer.Stores.Serialization;
|
||
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
||
|
namespace BlueWest.WebApi.Context.Users;
|
||
|
|
||
|
/// <summary>
|
||
|
/// Application User Db Context
|
||
|
/// </summary>
|
||
|
public class ApplicationUserDbContext : IdentityDbContext<
|
||
|
ApplicationUser,
|
||
|
ApplicationRole,
|
||
|
string,
|
||
|
ApplicationUserClaim,
|
||
|
ApplicationUserRole,
|
||
|
ApplicationUserLogin,
|
||
|
ApplicationRoleClaim,
|
||
|
ApplicationUserToken>, IPersistedGrantDbContext
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User roles.
|
||
|
/// </summary>
|
||
|
public override DbSet<ApplicationUserRole> UserRoles { get; set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets or sets the <see cref="DbSet{TEntity}"/> of roles.
|
||
|
/// </summary>
|
||
|
public override DbSet<ApplicationRole> Roles { get; set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets or sets the <see cref="DbSet{TEntity}"/> of role claims.
|
||
|
/// </summary>
|
||
|
public override DbSet<ApplicationRoleClaim> RoleClaims { get; set; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// Configures the schema needed for the identity framework.
|
||
|
/// </summary>
|
||
|
/// <param name="builder">
|
||
|
/// The builder being used to construct the model for this context.
|
||
|
/// </param>
|
||
|
|
||
|
/// <summary>
|
||
|
/// Database for the context of database users
|
||
|
/// </summary>
|
||
|
/// <param name="options"></param>
|
||
|
public ApplicationUserDbContext(DbContextOptions<UserDbContext> options) : base(options)
|
||
|
{
|
||
|
Database.EnsureCreated();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Configures the schema needed for the identity framework.
|
||
|
/// </summary>
|
||
|
/// <param name="builder">
|
||
|
/// The builder being used to construct the model for this context.
|
||
|
/// </param>
|
||
|
protected override void OnModelCreating(ModelBuilder builder)
|
||
|
{
|
||
|
base.OnModelCreating(builder);
|
||
|
builder.ConfigureCurrentDbModel();
|
||
|
base.OnModelCreating(builder);
|
||
|
builder.Entity<ApplicationUser>(b =>
|
||
|
{
|
||
|
b.HasMany<ApplicationUserRole>().WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
|
||
|
});
|
||
|
|
||
|
builder.Entity<ApplicationRole>(b =>
|
||
|
{
|
||
|
b.HasKey(r => r.Id);
|
||
|
b.HasIndex(r => r.NormalizedName).HasDatabaseName("RoleNameIndex").IsUnique();
|
||
|
b.ToTable("Roles");
|
||
|
b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();
|
||
|
|
||
|
b.Property(u => u.Name).HasMaxLength(256);
|
||
|
b.Property(u => u.NormalizedName).HasMaxLength(256);
|
||
|
|
||
|
b.HasMany<ApplicationUserRole>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
|
||
|
b.HasMany<ApplicationRoleClaim>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
|
||
|
});
|
||
|
|
||
|
builder.Entity<ApplicationRoleClaim>(b =>
|
||
|
{
|
||
|
b.HasKey(rc => rc.Id);
|
||
|
b.ToTable("RoleClaims");
|
||
|
});
|
||
|
|
||
|
builder.Entity<ApplicationUserRole>(b =>
|
||
|
{
|
||
|
b.HasKey(r => new { r.UserId, r.RoleId });
|
||
|
b.ToTable("UserRoles");
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public Task<int> SaveChangesAsync()
|
||
|
{
|
||
|
return SaveChangesAsync();
|
||
|
}
|
||
|
|
||
|
public DbSet<PersistedGrant> PersistedGrants { get; set; }
|
||
|
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }
|
||
|
public DbSet<Key> Keys { get; set; }
|
||
|
}
|