using BlueWest.Data;
using BlueWest.WebApi.Context.Users;
using BlueWest.WebApi.EF.Model;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.Context;
///
/// Application User Db Context
///
public class ApplicationUserDbContext : IdentityDbContext<
ApplicationUser,
ApplicationRole,
string,
ApplicationUserClaim,
ApplicationUserRole,
ApplicationUserLogin,
ApplicationRoleClaim,
ApplicationUserToken>
{
///
public sealed override DbSet UserClaims { get; set; }
///
public sealed override DbSet UserRoles { get; set; }
///
public sealed override DbSet Roles { get; set; }
///
public sealed override DbSet RoleClaims { get; set; }
///
public ApplicationUserDbContext(DbContextOptions options) : base(options)
{
Database.EnsureCreated();
}
///
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity().ToTable("UserRoles");
builder.Entity(b =>
{
b.HasMany()
.WithOne()
.HasForeignKey(ur => ur.UserId).IsRequired();
});
builder.Entity().ToTable("ApplicationUser")
.HasKey(x => x.Id);
builder.Entity(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().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
b.HasMany().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
});
builder.Entity(b =>
{
b.HasKey(rc => rc.Id);
b.ToTable("RoleClaims");
});
builder.Entity(b =>
{
b.HasKey(r => new { r.UserId, r.RoleId });
b.ToTable("UserRoles");
});
builder.Entity(b => b.HasOne()
.WithMany(x => x.Users)
.HasForeignKey(x => x.ApplicationUserId));
builder.Entity().ToTable("RoleClaims");
builder.Entity().ToTable("UserRole");
builder.ConfigureCurrentDbModel();
}
}