using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using ROLAC.API.Entities; namespace ROLAC.API.Data; public class AppDbContext : IdentityDbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet RefreshTokens => Set(); protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity(entity => { entity.HasKey(e => e.Id); // Unique index on hash — enables fast lookup and prevents duplicate tokens entity.HasIndex(e => e.TokenHash).IsUnique(); entity.Property(e => e.TokenHash).HasMaxLength(64).IsRequired(); entity.Property(e => e.UserId).HasMaxLength(450).IsRequired(); entity.Property(e => e.DeviceInfo).HasMaxLength(200); entity.Property(e => e.IpAddress).HasMaxLength(45); entity.Property(e => e.ReplacedByHash).HasMaxLength(64); entity.HasOne(e => e.User) .WithMany(u => u.RefreshTokens) .HasForeignKey(e => e.UserId) .OnDelete(DeleteBehavior.Cascade); // Computed properties are not DB columns entity.Ignore(e => e.IsExpired); entity.Ignore(e => e.IsRevoked); entity.Ignore(e => e.IsActive); }); builder.Entity(entity => { entity.Property(e => e.LanguagePreference).HasMaxLength(10).HasDefaultValue("en"); }); builder.Entity(entity => { entity.Property(e => e.Description).HasMaxLength(500); }); } }