KoogleApp/Koogle.Infrastrcuture/Data/Configurations/DayPersonConfiguration.cs

47 lines
1.5 KiB
C#

using Koogle.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Koogle.Infrastructure.Data.Configurations;
public class DayPersonConfiguration : IEntityTypeConfiguration<DayPerson>
{
public void Configure(EntityTypeBuilder<DayPerson> builder)
{
builder.ToTable("DayPersons");
// Ein Person genau einmal pro Day
builder.HasKey(x => new { x.DayId, x.PersonId });
builder.Property(x => x.ClubId).IsRequired();
builder.HasOne(x => x.Club)
.WithMany()
.HasForeignKey(x => x.ClubId)
.OnDelete(DeleteBehavior.Cascade);
// Club-Konsistenz: (DayId, ClubId) muss in Day(Id, ClubId) existieren
builder.HasOne(x => x.Day)
.WithMany(x => x.DayPersons)
.HasForeignKey(x => new { x.DayId, x.ClubId })
.HasPrincipalKey(d => new { d.Id, d.ClubId })
.OnDelete(DeleteBehavior.Cascade);
// Club-Konsistenz: (PersonId, ClubId) muss in Person(Id, ClubId) existieren
builder.HasOne(x => x.Person)
.WithMany(x => x.DayPersons)
.HasForeignKey(x => new { x.PersonId, x.ClubId })
.HasPrincipalKey(p => new { p.Id, p.ClubId })
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(x => x.ClubId);
builder.HasIndex(x => x.DayId);
}
}