1
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Domain.Common;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Domain.System;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Jiaowu.Api.Infrastructure.Persistence;
|
||||
|
||||
public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
: IdentityDbContext<ApplicationUser, ApplicationRole, Guid>(options)
|
||||
{
|
||||
public DbSet<Campus> Campuses => Set<Campus>();
|
||||
public DbSet<College> Colleges => Set<College>();
|
||||
public DbSet<Major> Majors => Set<Major>();
|
||||
public DbSet<AdministrativeClass> AdministrativeClasses => Set<AdministrativeClass>();
|
||||
public DbSet<Building> Buildings => Set<Building>();
|
||||
public DbSet<Classroom> Classrooms => Set<Classroom>();
|
||||
public DbSet<AcademicTerm> AcademicTerms => Set<AcademicTerm>();
|
||||
public DbSet<AuditLog> AuditLogs => Set<AuditLog>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
base.OnModelCreating(builder);
|
||||
|
||||
builder.Entity<ApplicationUser>(entity =>
|
||||
{
|
||||
entity.Property(x => x.DisplayName).HasMaxLength(50);
|
||||
entity.Property(x => x.StaffNumber).HasMaxLength(30);
|
||||
entity.HasIndex(x => x.StaffNumber);
|
||||
});
|
||||
|
||||
builder.Entity<ApplicationRole>(entity =>
|
||||
{
|
||||
entity.Property(x => x.Description).HasMaxLength(100);
|
||||
});
|
||||
|
||||
ConfigureCatalog<Campus>(builder);
|
||||
ConfigureCatalog<College>(builder);
|
||||
ConfigureCatalog<Major>(builder);
|
||||
ConfigureCatalog<AdministrativeClass>(builder);
|
||||
ConfigureCatalog<Building>(builder);
|
||||
ConfigureCatalog<Classroom>(builder);
|
||||
ConfigureCatalog<AcademicTerm>(builder);
|
||||
|
||||
builder.Entity<College>()
|
||||
.HasOne(x => x.Campus)
|
||||
.WithMany()
|
||||
.HasForeignKey(x => x.CampusId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
builder.Entity<Major>()
|
||||
.HasOne(x => x.College)
|
||||
.WithMany()
|
||||
.HasForeignKey(x => x.CollegeId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
builder.Entity<AdministrativeClass>()
|
||||
.HasOne(x => x.Major)
|
||||
.WithMany()
|
||||
.HasForeignKey(x => x.MajorId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
builder.Entity<Building>()
|
||||
.HasOne(x => x.Campus)
|
||||
.WithMany()
|
||||
.HasForeignKey(x => x.CampusId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
builder.Entity<Classroom>()
|
||||
.HasOne(x => x.Building)
|
||||
.WithMany()
|
||||
.HasForeignKey(x => x.BuildingId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
builder.Entity<AcademicTerm>()
|
||||
.HasIndex(x => x.IsCurrent);
|
||||
|
||||
builder.Entity<AuditLog>(entity =>
|
||||
{
|
||||
entity.Property(x => x.Method).HasMaxLength(10);
|
||||
entity.Property(x => x.Path).HasMaxLength(300);
|
||||
entity.Property(x => x.UserName).HasMaxLength(100);
|
||||
entity.Property(x => x.IpAddress).HasMaxLength(64);
|
||||
entity.HasIndex(x => x.CreatedAt);
|
||||
});
|
||||
}
|
||||
|
||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
foreach (var entry in ChangeTracker.Entries<EntityBase>()
|
||||
.Where(x => x.State == EntityState.Modified))
|
||||
{
|
||||
entry.Entity.UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
return base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private static void ConfigureCatalog<TEntity>(ModelBuilder builder)
|
||||
where TEntity : CatalogEntity
|
||||
{
|
||||
builder.Entity<TEntity>(entity =>
|
||||
{
|
||||
entity.Property(x => x.Code).HasMaxLength(40);
|
||||
entity.Property(x => x.Name).HasMaxLength(100);
|
||||
entity.HasIndex(x => x.Code).IsUnique();
|
||||
entity.HasIndex(x => new { x.IsEnabled, x.SortOrder });
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user