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

116 lines
4.0 KiB
C#

using BlueWest.Data;
using BlueWest.EfMethods;
using BlueWest.WebApi.Context.Users;
using BlueWest.WebApi.EF.Model;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.Context
{
/// <summary>
/// Application User Db Context
/// </summary>
[EfGenerator]
public class ApplicationUserDbContext : IdentityDbContext<
ApplicationUser,
ApplicationRole,
string,
ApplicationUserClaim,
ApplicationUserRole,
ApplicationUserLogin,
ApplicationRoleClaim,
ApplicationUserToken>
{
/// <inheritdoc />
[EfGetMany(typeof(ApplicationUserClaimUnique))]
public sealed override DbSet<ApplicationUserClaim> UserClaims { get; set; }
[EfGetMany(typeof(ApplicationUserRoleUnique))]
/// <inheritdoc />
public sealed override DbSet<ApplicationUserRole> UserRoles { get; set; }
[EfGetMany(typeof(ApplicationRoleUnique))]
/// <inheritdoc />
public sealed override DbSet<ApplicationRole> Roles { get; set; }
[EfGetMany(typeof(ApplicationRoleClaimUnique))]
/// <inheritdoc />
public sealed override DbSet<ApplicationRoleClaim> RoleClaims { get; set; }
[EfGetMany(typeof(ApplicationUserUnique))]
[EfUpdateMethods( updateType: typeof(ApplicationUserUnique), returnType: typeof(ApplicationUserUnique), keyPropertyMemberName: nameof(ApplicationUserUnique.Id))]
public sealed override DbSet<ApplicationUser> Users { get; set; }
#region Initialization
/// <inheritdoc />
public ApplicationUserDbContext(DbContextOptions<ApplicationUserDbContext> options) : base(options)
{
Database.EnsureCreated();
}
/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
builder.Entity<ApplicationUser>(b =>
{
b.HasMany<ApplicationUserRole>()
.WithOne()
.HasForeignKey(ur => ur.UserId).IsRequired();
});
builder.Entity<ApplicationUser>().ToTable("ApplicationUser")
.HasKey(x => x.Id);
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<ApplicationUserRole>().HasOne<ApplicationRole>(x => x.ApplicationRole);
builder.Entity<ApplicationRoleClaim>().HasOne<ApplicationRole>(x => x.ApplicationRole);
builder.Entity<ApplicationUserClaim>().HasOne<ApplicationUser>(x => x.ApplicationUser);
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");
});
builder.Entity<User>(b => b.HasOne<ApplicationUser>()
.WithMany(x => x.Users)
.HasForeignKey(x => x.ApplicationUserId));
builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
builder.Entity<ApplicationUserRole>().ToTable("UserRole");
builder.ConfigureCurrentDbModel();
}
#endregion
/// <inheritdoc />
}
}