using xIdentityModels; using xIdentityModels.Models; using xIdentityModels.Navigations; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; namespace xIds.DbContext { public class XIdentityDbContext : IdentityDbContext { // #region Props/DbSets ... public DbSet Tokens { get; set; } public DbSet Devices { get; set; } public DbSet ApiKeys { get; set; } public DbSet Avatars { get; set; } public DbSet ApiKeyUsage { get; set; } public DbSet Applications { get; set; } public DbSet BannedDevices { get; set; } public DbSet Followers { get; set; } public DbSet Followings { get; set; } public DbSet VerificationRequests { get; set; } #endregion public XIdentityDbContext(DbContextOptions options) : base(options) { // try { Database.EnsureCreated(); Database.Migrate(); } catch { } } protected override void OnModelCreating(ModelBuilder modelBuilder) { // base.OnModelCreating(modelBuilder); // // Fix XUser GUID Generator on Add ... modelBuilder .Entity() .Property(e => e.Id) .ValueGeneratedOnAdd(); // // Handle Role List ... modelBuilder.Entity(u => { u.HasMany(x => x.Roles) .WithOne() .HasForeignKey(ur => ur.UserId) .IsRequired(); }); // // Configure XApplication ... modelBuilder.Entity(e => { e.HasIndex(a => a.Name).IsUnique(); e.HasMany(a => a.ApiKeys) .WithOne(k => k.Application) .HasForeignKey(k => k.ApplicationId); }); // // Configure XApiKey ... modelBuilder.Entity(e => { e.HasIndex(k => k.KeyHash).IsUnique(); e.HasIndex(k => k.ExpiresAt); }); } } }