using back.DataModels; using Microsoft.EntityFrameworkCore; namespace back.persistance.data.relations; public class GalleryRelationEstablisher : IRelationEstablisher { public void EstablishRelation(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => { entity.Property(e => e.IsArchived).HasDefaultValue(0); entity.Property(e => e.IsFavorite).HasDefaultValue(0); entity.Property(e => e.IsPublic).HasDefaultValue(1); entity.HasOne(d => d.CreatedByNavigation).WithMany(p => p.Galleries) .HasForeignKey(d => d.CreatedBy) .OnDelete(DeleteBehavior.ClientSetNull); entity.HasOne(d => d.Event).WithMany(p => p.Galleries).HasForeignKey(d => d.EventId); entity.HasMany(d => d.Photos).WithMany(p => p.Galleries) .UsingEntity>( "GalleryPhoto", r => r.HasOne().WithMany() .HasForeignKey("PhotoId") .OnDelete(DeleteBehavior.ClientSetNull), l => l.HasOne().WithMany() .HasForeignKey("GalleryId") .OnDelete(DeleteBehavior.ClientSetNull), j => { j.HasKey("GalleryId", "PhotoId"); j.ToTable("GalleryPhotos"); }); entity.HasMany(d => d.Tags).WithMany(p => p.Galleries) .UsingEntity>( "GalleryTag", r => r.HasOne().WithMany() .HasForeignKey("TagId") .OnDelete(DeleteBehavior.ClientSetNull), l => l.HasOne().WithMany() .HasForeignKey("GalleryId") .OnDelete(DeleteBehavior.ClientSetNull), j => { j.HasKey("GalleryId", "TagId"); j.ToTable("GalleryTags"); }); entity.HasMany(d => d.Users).WithMany(p => p.GalleriesNavigation) .UsingEntity>( "GalleryUserViewer", r => r.HasOne().WithMany() .HasForeignKey("UserId") .OnDelete(DeleteBehavior.ClientSetNull), l => l.HasOne().WithMany() .HasForeignKey("GalleryId") .OnDelete(DeleteBehavior.ClientSetNull), j => { j.HasKey("GalleryId", "UserId"); j.ToTable("GalleryUserViewers"); }); }); } }