Database improvement
1. Separate configuration files 2. Remove commented code 3. Improve entity configurations (constraints, indexes, relationships)
This commit is contained in:
@@ -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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user