Files
xSaherElmIds/DbContext/XIdentityDbContext.cs
T
2026-10-04 18:11:46 +03:30

78 lines
2.4 KiB
C#

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<XToken> Tokens { get; set; }
public DbSet<XDevice> Devices { get; set; }
public DbSet<XApiKey> ApiKeys { get; set; }
public DbSet<XProfileImage> Avatars { get; set; }
public DbSet<XApiKeyUsage> ApiKeyUsage { get; set; }
public DbSet<XApplication> Applications { get; set; }
public DbSet<XBannedDevice> BannedDevices { get; set; }
public DbSet<XFriendshipFollower> Followers { get; set; }
public DbSet<XFriendshipFollowing> Followings { get; set; }
public DbSet<XVerificationRequest> VerificationRequests { get; set; }
#endregion
public XIdentityDbContext(DbContextOptions<XIdentityDbContext> 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<IdentityUser>()
.Property(e => e.Id)
.ValueGeneratedOnAdd();
//
// Handle Role List ...
modelBuilder.Entity<XUser>(u =>
{
u.HasMany(x => x.Roles)
.WithOne()
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
//
// Configure XApplication ...
modelBuilder.Entity<XApplication>(e =>
{
e.HasIndex(a => a.Name).IsUnique();
e.HasMany(a => a.ApiKeys)
.WithOne(k => k.Application)
.HasForeignKey(k => k.ApplicationId);
});
//
// Configure XApiKey ...
modelBuilder.Entity<XApiKey>(e =>
{
e.HasIndex(k => k.KeyHash).IsUnique();
e.HasIndex(k => k.ExpiresAt);
});
}
}
}