using ErsatzTV.Core.Domain; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace ErsatzTV.Infrastructure.Data.Configurations; public class ChannelConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.ToTable("Channel"); builder.HasIndex(c => c.Number) .IsUnique(); builder.Property(c => c.Name) .HasMaxLength(50) .HasColumnType("varchar(50)"); builder.Property(c => c.IsEnabled) .HasDefaultValue(true); builder.Property(c => c.ShowInEpg) .HasDefaultValue(true); builder.HasMany(c => c.Playouts) // TODO: is this correct, or should we have one to one? .WithOne(p => p.Channel) .HasForeignKey(p => p.ChannelId) .OnDelete(DeleteBehavior.Cascade); builder.HasMany(c => c.Artwork) .WithOne() .OnDelete(DeleteBehavior.Cascade); builder.HasOne(i => i.Watermark) .WithMany() .HasForeignKey(i => i.WatermarkId) .OnDelete(DeleteBehavior.SetNull) .IsRequired(false); builder.HasOne(i => i.FallbackFiller) .WithMany() .HasForeignKey(i => i.FallbackFillerId) .OnDelete(DeleteBehavior.SetNull) .IsRequired(false); builder.Property(c => c.Group) .IsRequired() .HasDefaultValue("ErsatzTV"); builder.HasOne(i => i.MirrorSourceChannel) .WithMany() .HasForeignKey(i => i.MirrorSourceChannelId) .OnDelete(DeleteBehavior.SetNull) .IsRequired(false); builder.HasMany(c => c.GraphicsElements) .WithMany(m => m.Channels) .UsingEntity( j => j.HasOne(ci => ci.GraphicsElement) .WithMany(mi => mi.ChannelGraphicsElements) .HasForeignKey(ci => ci.GraphicsElementId) .OnDelete(DeleteBehavior.Cascade), j => j.HasOne(ci => ci.Channel) .WithMany(c => c.ChannelGraphicsElements) .HasForeignKey(ci => ci.ChannelId) .OnDelete(DeleteBehavior.Cascade), j => j.HasKey(ci => new { ci.ChannelId, ci.GraphicsElementId })); } }