Files
mmorales.photo/back/persistance/data/relations/GalleryRelationEstablisher.cs
2025-08-24 14:18:20 +02:00

68 lines
2.8 KiB
C#

using back.DataModels;
using Microsoft.EntityFrameworkCore;
namespace back.persistance.data.relations;
public class GalleryRelationEstablisher : IRelationEstablisher
{
public void EstablishRelation(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Gallery>(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<Dictionary<string, object>>(
"GalleryPhoto",
r => r.HasOne<Photo>().WithMany()
.HasForeignKey("PhotoId")
.OnDelete(DeleteBehavior.ClientSetNull),
l => l.HasOne<Gallery>().WithMany()
.HasForeignKey("GalleryId")
.OnDelete(DeleteBehavior.ClientSetNull),
j =>
{
j.HasKey("GalleryId", "PhotoId");
j.ToTable("GalleryPhotos");
});
entity.HasMany(d => d.Tags).WithMany(p => p.Galleries)
.UsingEntity<Dictionary<string, object>>(
"GalleryTag",
r => r.HasOne<Tag>().WithMany()
.HasForeignKey("TagId")
.OnDelete(DeleteBehavior.ClientSetNull),
l => l.HasOne<Gallery>().WithMany()
.HasForeignKey("GalleryId")
.OnDelete(DeleteBehavior.ClientSetNull),
j =>
{
j.HasKey("GalleryId", "TagId");
j.ToTable("GalleryTags");
});
entity.HasMany(d => d.Users).WithMany(p => p.GalleriesNavigation)
.UsingEntity<Dictionary<string, object>>(
"GalleryUserViewer",
r => r.HasOne<User>().WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.ClientSetNull),
l => l.HasOne<Gallery>().WithMany()
.HasForeignKey("GalleryId")
.OnDelete(DeleteBehavior.ClientSetNull),
j =>
{
j.HasKey("GalleryId", "UserId");
j.ToTable("GalleryUserViewers");
});
});
}
}