using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace Roadie.Library.Identity { public class ApplicationUserDbContext : IdentityDbContext< ApplicationUser, ApplicationRole, int, ApplicationUserClaim, ApplicationUserRole, IdentityUserLogin, ApplicationRoleClaim, IdentityUserToken> { public ApplicationUserDbContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity(b => { b.ToTable("user"); // Each User can have many UserClaims b.HasMany(e => e.Claims) .WithOne(e => e.User) .HasForeignKey(uc => uc.UserId) .IsRequired(); // Each User can have many entries in the UserRole join table b.HasMany(e => e.UserRoles) .WithOne(e => e.User) .HasForeignKey(ur => ur.UserId) .IsRequired(); }); builder.Entity(b => { b.ToTable("userrole"); b.HasKey(ar => ar.Id); // Each Role can have many entries in the UserRole join table b.HasMany(e => e.UserRoles) .WithOne(e => e.Role) .HasForeignKey(ur => ur.RoleId) .IsRequired(); // Each Role can have many associated RoleClaims b.HasMany(e => e.RoleClaims) .WithOne(e => e.Role) .HasForeignKey(rc => rc.RoleId) .IsRequired(); }); builder.Entity(b => { b.ToTable("userClaims"); }); builder.Entity(b => { b.ToTable("usersInRoles"); }); builder.Entity(b => { b.ToTable("userRoleClaims"); }); } } }