Files
chapter-organizer/Data/Configurations/StudentConfiguration.cs
T
poprhythm 826eac1372 Database improvement
1.  Separate configuration files
2.  Remove commented code
3.  Improve entity configurations (constraints, indexes, relationships)
2025-12-04 08:09:01 -05:00

61 lines
1.8 KiB
C#

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>();
}
}
}