From 7f5ecb9d3a620b57e4d94614fa55cc229a8b7873 Mon Sep 17 00:00:00 2001 From: Kevin Matsubara Date: Sun, 6 Apr 2025 21:06:59 +0200 Subject: [PATCH] Add Configurations for Teams and Leagues. --- .../Configurations/LeagueConfiguration.cs | 18 +++++++++++ .../Configurations/TeamConfiguration.cs | 30 +++++++++++++++++++ .../DeadBallZoneLeagueDbContext.cs | 24 ++++----------- 3 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 EntityFrameworkCore.Data/Configurations/LeagueConfiguration.cs create mode 100644 EntityFrameworkCore.Data/Configurations/TeamConfiguration.cs diff --git a/EntityFrameworkCore.Data/Configurations/LeagueConfiguration.cs b/EntityFrameworkCore.Data/Configurations/LeagueConfiguration.cs new file mode 100644 index 0000000..93782e0 --- /dev/null +++ b/EntityFrameworkCore.Data/Configurations/LeagueConfiguration.cs @@ -0,0 +1,18 @@ +using EntityFrameworkCore.Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace EntityFrameworkCore.Data; + +public class LeagueConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasData( + new League { Id = 1, Name = "Local League", CreatedDate = new DateTime(2025, 4, 6, 17, 7, 27, 33)}, + new League { Id = 2, Name = "National League", CreatedDate = new DateTime(2025, 4, 6, 17, 7, 27, 33)}, + new League { Id = 3, Name = "Geosphere", CreatedDate = new DateTime(2025, 4, 6, 17, 7, 27, 33)}, + new League { Id = 4, Name = "Cyber war", CreatedDate = new DateTime(2025, 4, 6, 17, 7, 27, 33)} + ); + } +} diff --git a/EntityFrameworkCore.Data/Configurations/TeamConfiguration.cs b/EntityFrameworkCore.Data/Configurations/TeamConfiguration.cs new file mode 100644 index 0000000..88968b4 --- /dev/null +++ b/EntityFrameworkCore.Data/Configurations/TeamConfiguration.cs @@ -0,0 +1,30 @@ +using EntityFrameworkCore.Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace EntityFrameworkCore.Data; + +public class TeamConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasData( + // Use hardcoded dates, not dynamic data such as new DateTimeOffset.UtcNow.DateTime for seeding. + new Team { Id = 1, Name = "Neo Delhi", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33)}, + new Team { Id = 2, Name = "Voodoo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 3, Name = "Penal X", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 4, Name = "Neo Tokyo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 5, Name = "Neo Barcelona", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 6, Name = "Neo Manchester", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 7, Name = "Neo Bangkok", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 8, Name = "Neo Amsterdam", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 9, Name = "Killaklowns", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 10, Name = "Sol", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 11, Name = "DEC", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 12, Name = "Leopards", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 13, Name = "Harlequins", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 14, Name = "Gladiators", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, + new Team { Id = 15, Name = "Fiz-O", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) } + ); + } +} diff --git a/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs b/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs index 7fdbf10..d30cfc1 100644 --- a/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs +++ b/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs @@ -2,6 +2,7 @@ using EntityFrameworkCore.Domain; using Microsoft.Extensions.Logging; +using System.Reflection; namespace EntityFrameworkCore.Data; @@ -31,23 +32,10 @@ public class DeadBallZoneLeagueDbContext : DbContext protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity().HasData( - // Use hardcoded dates, not dynamic data such as new DateTimeOffset.UtcNow.DateTime for seeding. - new Team { Id = 1, Name = "Neo Delhi", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33)}, - new Team { Id = 2, Name = "Voodoo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 3, Name = "Penal X", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 4, Name = "Neo Tokyo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 5, Name = "Neo Barcelona", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 6, Name = "Neo Manchester", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 7, Name = "Neo Bangkok", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 8, Name = "Neo Amsterdam", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 9, Name = "Killaklowns", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 10, Name = "Sol", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 11, Name = "DEC", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 12, Name = "Leopards", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 13, Name = "Harlequins", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 14, Name = "Gladiators", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }, - new Team { Id = 15, Name = "Fiz-O", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) } - ); + // modelBuilder.ApplyConfiguration(new TeamConfiguration()); + // modelBuilder.ApplyConfiguration(new LeagueConfiguration()); + + // This line can be used, then individual configurations do not need to be loaded. + modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); } }