CodeLiturgy.Dashboard/BlueWest.Api/Context/ApplicationUserDbContext.cs

97 lines
2.8 KiB
C#
Raw Normal View History

2022-09-10 00:33:17 +03:00
using BlueWest.Data;
2022-09-11 01:22:04 +03:00
using BlueWest.WebApi.Context.Users;
2022-09-10 00:33:17 +03:00
using BlueWest.WebApi.EF.Model;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
2022-09-11 01:22:04 +03:00
namespace BlueWest.WebApi.Context;
2022-09-10 00:33:17 +03:00
/// <summary>
/// Application User Db Context
/// </summary>
public class ApplicationUserDbContext : IdentityDbContext<
ApplicationUser,
ApplicationRole,
string,
ApplicationUserClaim,
ApplicationUserRole,
ApplicationUserLogin,
ApplicationRoleClaim,
2022-09-10 07:12:03 +03:00
ApplicationUserToken>
2022-09-10 00:33:17 +03:00
{
2022-09-11 01:22:04 +03:00
/// <inheritdoc />
public sealed override DbSet<ApplicationUserClaim> UserClaims { get; set; }
2022-09-10 00:33:17 +03:00
2022-09-11 01:22:04 +03:00
/// <inheritdoc />
public sealed override DbSet<ApplicationUserRole> UserRoles { get; set; }
/// <inheritdoc />
public sealed override DbSet<ApplicationRole> Roles { get; set; }
/// <inheritdoc />
public sealed override DbSet<ApplicationRoleClaim> RoleClaims { get; set; }
/// <inheritdoc />
2022-09-10 07:12:03 +03:00
public ApplicationUserDbContext(DbContextOptions<ApplicationUserDbContext> options) : base(options)
2022-09-10 00:33:17 +03:00
{
Database.EnsureCreated();
}
2022-09-11 01:22:04 +03:00
/// <inheritdoc />
2022-09-10 00:33:17 +03:00
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
2022-09-10 07:28:41 +03:00
builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
2022-09-10 00:33:17 +03:00
builder.Entity<ApplicationUser>(b =>
{
2022-09-10 07:12:03 +03:00
b.HasMany<ApplicationUserRole>()
.WithOne()
.HasForeignKey(ur => ur.UserId).IsRequired();
2022-09-10 00:33:17 +03:00
});
2022-09-10 07:12:03 +03:00
builder.Entity<ApplicationUser>().ToTable("ApplicationUser")
.HasKey(x => x.Id);
2022-09-10 07:28:41 +03:00
2022-09-10 07:12:03 +03:00
2022-09-10 00:33:17 +03:00
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");
});
2022-09-11 01:22:04 +03:00
2022-09-10 07:28:41 +03:00
builder.Entity<User>(b => b.HasOne<ApplicationUser>()
.WithMany(x => x.Users)
.HasForeignKey(x => x.ApplicationUserId));
2022-09-11 01:22:04 +03:00
builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
builder.Entity<ApplicationUserRole>().ToTable("UserRole");
2022-09-10 07:12:03 +03:00
builder.ConfigureCurrentDbModel();
2022-09-10 00:33:17 +03:00
}
}