Add restrictions in configuration and data annotations.

This commit is contained in:
Kevin Matsubara 2025-04-14 21:29:56 +02:00
parent 5b90fa1242
commit e4b92dedef
3 changed files with 26 additions and 1 deletions

View File

@ -10,6 +10,11 @@ public class TeamConfiguration : IEntityTypeConfiguration<Team>
{
builder.HasIndex(t => t.Name).IsUnique();
// example composite key configuration.
// builder.HasIndex(λ => new { λ.CoachId, λ.LeagueId }).IsUnique();
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)

View File

@ -37,6 +37,7 @@ namespace EntityFrameworkCore.Data.Migrations
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("TEXT");
b.HasKey("Id");
@ -277,6 +278,8 @@ namespace EntityFrameworkCore.Data.Migrations
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("TEXT");
b.HasKey("Id");
@ -429,6 +432,19 @@ namespace EntityFrameworkCore.Data.Migrations
});
});
modelBuilder.Entity("EntityFrameworkCore.Domain.TeamsAndLeaguesView", b =>
{
b.Property<string>("LeagueName")
.HasColumnType("TEXT");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.ToTable((string)null);
b.ToView("vw_TeamsAndLeagues", (string)null);
});
modelBuilder.Entity("EntityFrameworkCore.Domain.Match", b =>
{
b.HasOne("EntityFrameworkCore.Domain.Team", "AwayTeam")

View File

@ -1,7 +1,11 @@
namespace EntityFrameworkCore.Domain;
using System.ComponentModel.DataAnnotations;
namespace EntityFrameworkCore.Domain;
// Since C#11, you do not need to wrap in accolades anymore.
public class Coach : BaseDomainModel
{
[MaxLength(100)]
[Required]
public string Name { get; set; } // Strings are automatically become VARCHAR database types.
public Team? Team { get; set; }
}