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

69 lines
2.8 KiB
C#

using back.DataModels;
using Microsoft.EntityFrameworkCore;
namespace back.persistance.data.relations;
public class PersonRelationEstablisher : IRelationEstablisher
{
public void EstablishRelation(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Photo>(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.Photos)
.HasForeignKey(d => d.CreatedBy)
.OnDelete(DeleteBehavior.ClientSetNull);
entity.HasOne(d => d.Event).WithMany(p => p.Photos).HasForeignKey(d => d.EventId);
entity.HasMany(d => d.People).WithMany(p => p.PhotosNavigation)
.UsingEntity<Dictionary<string, object>>(
"PhotoPerson",
r => r.HasOne<Person>().WithMany()
.HasForeignKey("PersonId")
.OnDelete(DeleteBehavior.ClientSetNull),
l => l.HasOne<Photo>().WithMany()
.HasForeignKey("PhotoId")
.OnDelete(DeleteBehavior.ClientSetNull),
j =>
{
j.HasKey("PhotoId", "PersonId");
j.ToTable("PhotoPersons");
});
entity.HasMany(d => d.Tags).WithMany(p => p.Photos)
.UsingEntity<Dictionary<string, object>>(
"PhotoTag",
r => r.HasOne<Tag>().WithMany()
.HasForeignKey("TagId")
.OnDelete(DeleteBehavior.ClientSetNull),
l => l.HasOne<Photo>().WithMany()
.HasForeignKey("PhotoId")
.OnDelete(DeleteBehavior.ClientSetNull),
j =>
{
j.HasKey("PhotoId", "TagId");
j.ToTable("PhotoTags");
});
entity.HasMany(d => d.Users).WithMany(p => p.Photos)
.UsingEntity<Dictionary<string, object>>(
"PhotoUserBuyer",
r => r.HasOne<User>().WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.ClientSetNull),
l => l.HasOne<Photo>().WithMany()
.HasForeignKey("PhotoId")
.OnDelete(DeleteBehavior.ClientSetNull),
j =>
{
j.HasKey("PhotoId", "UserId");
j.ToTable("PhotoUserBuyers");
});
});
}
}