See also: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=data-annotations
56 lines
3.3 KiB
C#
56 lines
3.3 KiB
C#
using EntityFrameworkCore.Domain;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
|
|
|
namespace EntityFrameworkCore.Data;
|
|
|
|
public class TeamConfiguration : IEntityTypeConfiguration<Team>
|
|
{
|
|
public void Configure(EntityTypeBuilder<Team> builder)
|
|
{
|
|
builder.HasIndex(t => t.Name).IsUnique();
|
|
|
|
// example composite key configuration.
|
|
// builder.HasIndex(λ => new { λ.CoachId, λ.LeagueId }).IsUnique();
|
|
|
|
// SQL Server method to setup concurrency.
|
|
// builder.Property(λ => λ.Version).IsRowVersion();
|
|
|
|
// SQLite (and other) method to setup concurrency. See also BaseDomain model.
|
|
builder.Property(λ => λ.Version).IsConcurrencyToken();
|
|
|
|
builder.Property(λ => λ.Name).HasMaxLength(100).IsRequired();
|
|
|
|
builder.HasMany(m => m.HomeMatches) // A team has many home matches.
|
|
.WithOne(m => m.HomeTeam) // Mapped to the HomeTeam property.
|
|
.HasForeignKey(m => m.HomeTeamId)
|
|
.IsRequired()
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
builder.HasMany(m => m.AwayMatches) // A team has many home matches.
|
|
.WithOne(m => m.AwayTeam) // Mapped to the HomeTeam property.
|
|
.HasForeignKey(m => m.AwayTeamId)
|
|
.IsRequired()
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
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), LeagueId = 2, CoachId = 1 },
|
|
new Team { Id = 2, Name = "Voodoo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 1, CoachId = 2 },
|
|
new Team { Id = 3, Name = "Penal X", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 1, CoachId = 3 },
|
|
new Team { Id = 4, Name = "Neo Tokyo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 3, CoachId = 4 },
|
|
new Team { Id = 5, Name = "Neo Barcelona", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 2, CoachId = 5 },
|
|
new Team { Id = 6, Name = "Neo Manchester", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 2, CoachId = 6 },
|
|
new Team { Id = 7, Name = "Neo Bangkok", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 3, CoachId = 7 },
|
|
new Team { Id = 8, Name = "Neo Amsterdam", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 3, CoachId = 8 },
|
|
new Team { Id = 9, Name = "Killaklowns", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 1, CoachId = 9 },
|
|
new Team { Id = 10, Name = "Sol", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 1, CoachId = 10 },
|
|
new Team { Id = 11, Name = "DEC", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 4, CoachId = 11 },
|
|
new Team { Id = 12, Name = "Leopards", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 1, CoachId = 12 },
|
|
new Team { Id = 13, Name = "Harlequins", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 1, CoachId = 13 },
|
|
new Team { Id = 14, Name = "Gladiators", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 4, CoachId = 14 },
|
|
new Team { Id = 15, Name = "Fiz-O", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33), LeagueId = 1, CoachId = 15 }
|
|
);
|
|
}
|
|
}
|