Database improvement

1.  Separate configuration files
2.  Remove commented code
3.  Improve entity configurations (constraints, indexes, relationships)
This commit is contained in:
2025-12-04 08:09:01 -05:00
parent 2d5d075879
commit 826eac1372
8 changed files with 787 additions and 80 deletions
@@ -0,0 +1,47 @@
using Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Data.Configurations
{
public class EventDefinitionConfiguration : IEntityTypeConfiguration<EventDefinition>
{
public void Configure(EntityTypeBuilder<EventDefinition> builder)
{
builder.HasKey(e => e.Id);
// Indexes
builder.HasIndex(e => e.Name).IsUnique();
builder.HasIndex(e => e.EventFormat);
// Constraints
builder.Property(e => e.Name)
.IsRequired()
.HasMaxLength(128);
builder.Property(e => e.ShortName)
.HasMaxLength(50);
builder.Property(e => e.Description)
.HasMaxLength(1000);
builder.Property(e => e.Theme)
.HasMaxLength(500);
builder.Property(e => e.Eligibility)
.IsRequired()
.HasMaxLength(200);
builder.Property(e => e.SemifinalistActivity)
.HasMaxLength(500);
builder.Property(e => e.Documentation)
.HasMaxLength(500);
// Value conversions for enums
builder.Property(e => e.EventFormat)
.HasConversion<string>()
.HasMaxLength(50);
}
}
}
@@ -0,0 +1,60 @@
using Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Data.Configurations
{
public class StudentConfiguration : IEntityTypeConfiguration<Student>
{
public void Configure(EntityTypeBuilder<Student> builder)
{
builder.HasKey(s => s.Id);
// Indexes
builder.HasIndex(s => new { s.FirstName, s.LastName });
builder.HasIndex(s => s.Email);
builder.HasIndex(s => s.Grade);
// Constraints
builder.Property(s => s.FirstName)
.IsRequired()
.HasMaxLength(100);
builder.Property(s => s.LastName)
.IsRequired()
.HasMaxLength(100);
builder.Property(s => s.Email)
.HasMaxLength(255);
builder.Property(s => s.PhoneNumber)
.HasMaxLength(20);
builder.Property(s => s.RegionalId)
.HasMaxLength(50);
builder.Property(s => s.StateId)
.HasMaxLength(50);
builder.Property(s => s.NationalId)
.HasMaxLength(50);
// Value conversion for enum
builder.Property(s => s.OfficerRole)
.HasConversion<string>()
.HasMaxLength(50);
// Relationships
// Configure the collection navigation to the join entity
builder.HasMany(s => s.EventRankings)
.WithOne(r => r.Student)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
// Many-to-many through StudentEventRanking
builder.HasMany(s => s.RankedEvents)
.WithMany()
.UsingEntity<StudentEventRanking>();
}
}
}
@@ -0,0 +1,33 @@
using Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Data.Configurations
{
public class StudentEventRankingConfiguration : IEntityTypeConfiguration<StudentEventRanking>
{
public void Configure(EntityTypeBuilder<StudentEventRanking> builder)
{
// Note: Relationships are configured in StudentConfiguration
// This configuration only defines keys, indexes, and constraints
// Composite key on shadow properties created by EF Core
builder.HasKey("StudentId", "EventDefinitionId");
// Indexes on shadow properties
builder.HasIndex(r => r.Rank);
builder.HasIndex("StudentId");
builder.HasIndex("EventDefinitionId");
// Constraints
builder.Property(r => r.Rank)
.IsRequired();
// Relationship to EventDefinition (Student relationship is in StudentConfiguration)
builder.HasOne(r => r.EventDefinition)
.WithMany()
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
}
}
}
+37
View File
@@ -0,0 +1,37 @@
using Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Data.Configurations
{
public class TeamConfiguration : IEntityTypeConfiguration<Team>
{
public void Configure(EntityTypeBuilder<Team> builder)
{
builder.HasKey(t => t.Id);
// Indexes on shadow properties created by EF Core
builder.HasIndex("EventId");
builder.HasIndex("EventId", "Identifier");
// Constraints
builder.Property(t => t.Identifier)
.HasMaxLength(50);
// Relationships
builder.HasOne(t => t.Event)
.WithMany()
.IsRequired()
.OnDelete(DeleteBehavior.Restrict); // Don't delete teams when event is deleted
builder.HasMany(t => t.Students)
.WithMany(s => s.Teams)
.UsingEntity(j => j.ToTable("TeamStudents")); // Explicit table name
builder.HasOne(t => t.Captain)
.WithMany()
.IsRequired(false)
.OnDelete(DeleteBehavior.SetNull); // Set to null if captain is deleted
}
}
}