41 lines
1.6 KiB
C#
41 lines
1.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Socialize.Api.Modules.Organizations.Data;
|
|
|
|
public static class OrganizationModelConfiguration
|
|
{
|
|
public static ModelBuilder ConfigureOrganizationsModule(this ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<Organization>(organization =>
|
|
{
|
|
organization.ToTable("Organizations");
|
|
organization.HasKey(x => x.Id);
|
|
organization.Property(x => x.Name).HasMaxLength(256).IsRequired();
|
|
organization.Property(x => x.LogoUrl).HasMaxLength(2048);
|
|
organization.Property(x => x.CreatedAt)
|
|
.ValueGeneratedOnAdd()
|
|
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
|
organization.HasIndex(x => x.OwnerUserId);
|
|
});
|
|
|
|
modelBuilder.Entity<OrganizationMembership>(membership =>
|
|
{
|
|
membership.ToTable("OrganizationMemberships");
|
|
membership.HasKey(x => x.Id);
|
|
membership.Property(x => x.Role).HasMaxLength(64).IsRequired();
|
|
membership.Property(x => x.CreatedAt)
|
|
.ValueGeneratedOnAdd()
|
|
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
|
membership.HasIndex(x => x.OrganizationId);
|
|
membership.HasIndex(x => x.UserId);
|
|
membership.HasIndex(x => new { x.OrganizationId, x.UserId }).IsUnique();
|
|
membership.HasOne<Organization>()
|
|
.WithMany()
|
|
.HasForeignKey(x => x.OrganizationId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
});
|
|
|
|
return modelBuilder;
|
|
}
|
|
}
|