using Wishlist.Models; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace Wishlist.Data; public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public DbSet BlogPosts => Set(); public DbSet ApplicationUsers => Set(); public DbSet ApplicationRoles => Set(); public DbSet ApplicationUserClaims => Set(); public DbSet ApplicationUserRoles => Set(); public DbSet ApplicationUserLogins => Set(); public DbSet ApplicationRoleClaims => Set(); public DbSet ApplicationUserTokens => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //models modelBuilder.Entity(b => { // Primary key b.HasKey(u => u.Id); // Indexes for "normalized" username and email, to allow efficient lookups b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(); b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex"); // Maps to the AspNetUsers table b.ToTable("AspNetUsers"); // A concurrency token for use with the optimistic concurrency checking b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken(); // Limit the size of columns to use efficient database types b.Property(u => u.UserName).HasMaxLength(256); b.Property(u => u.NormalizedUserName).HasMaxLength(256); b.Property(u => u.Email).HasMaxLength(256); b.Property(u => u.NormalizedEmail).HasMaxLength(256); // The relationships between User and other entity types // Note that these relationships are configured with no navigation properties // Each User can have many UserClaims b.HasMany().WithOne().HasForeignKey(uc => uc.UserId).IsRequired(); // Each User can have many UserLogins b.HasMany().WithOne().HasForeignKey(ul => ul.UserId).IsRequired(); // Each User can have many UserTokens b.HasMany().WithOne().HasForeignKey(ut => ut.UserId).IsRequired(); // Each User can have many entries in the UserRole join table b.HasMany().WithOne().HasForeignKey(ur => ur.UserId).IsRequired(); }); modelBuilder.Entity(b => { // Primary key b.HasKey(uc => uc.Id); // Maps to the AspNetUserClaims table b.ToTable("AspNetUserClaims"); }); modelBuilder.Entity(b => { // Composite primary key consisting of the LoginProvider and the key to use // with that provider b.HasKey(l => new { l.LoginProvider, l.ProviderKey }); // Limit the size of the composite key columns due to common DB restrictions b.Property(l => l.LoginProvider).HasMaxLength(128); b.Property(l => l.ProviderKey).HasMaxLength(128); // Maps to the AspNetUserLogins table b.ToTable("AspNetUserLogins"); }); modelBuilder.Entity(b => { // Composite primary key consisting of the UserId, LoginProvider and Name b.HasKey(t => new { t.UserId, t.LoginProvider, t.Name }); // Limit the size of the composite key columns due to common DB restrictions //b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength); //b.Property(t => t.Name).HasMaxLength(maxKeyLength); // Maps to the AspNetUserTokens table b.ToTable("AspNetUserTokens"); }); modelBuilder.Entity(b => { // Primary key b.HasKey(r => r.Id); // Index for "normalized" role name to allow efficient lookups b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique(); // Maps to the AspNetRoles table b.ToTable("AspNetRoles"); // A concurrency token for use with the optimistic concurrency checking b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken(); // Limit the size of columns to use efficient database types b.Property(u => u.Id).HasMaxLength(50); b.Property(u => u.Name).HasMaxLength(256); b.Property(u => u.NormalizedName).HasMaxLength(256); // The relationships between Role and other entity types // Note that these relationships are configured with no navigation properties // Each Role can have many entries in the UserRole join table b.HasMany().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired(); // Each Role can have many associated RoleClaims b.HasMany().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired(); }); modelBuilder.Entity(b => { // Primary key b.HasKey(rc => rc.Id); // Maps to the AspNetRoleClaims table b.ToTable("AspNetRoleClaims"); }); modelBuilder.Entity(b => { // Primary key b.HasKey(r => new { r.UserId, r.RoleId }); // Maps to the AspNetUserRoles table b.ToTable("AspNetUserRoles"); }); // navigation props modelBuilder.Entity(b => { // 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 UserLogins b.HasMany(e => e.Logins) .WithOne(e => e.User) .HasForeignKey(ul => ul.UserId) .IsRequired(); // Each User can have many UserTokens b.HasMany(e => e.Tokens) .WithOne(e => e.User) .HasForeignKey(ut => ut.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(); }); modelBuilder.Entity(b => { // 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(); }); } }