using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Prefab.Base.Catalog.Products; using Prefab.Catalog.Domain.Entities; namespace Prefab.Catalog.Data.Configs; public sealed class ProductConfig() : Prefab.Data.Configs.EntityConfig("Products", IModuleDb.SchemaName.ToLower()) { public override void Configure(EntityTypeBuilder builder) { base.Configure(builder); builder.HasKey(p => p.Id); builder.Property(p => p.RowVersion) .IsRowVersion(); builder.Property(p => p.Name) .HasMaxLength(ProductRules.NameMaxLength) .IsRequired(); builder.Property(p => p.Slug) .HasMaxLength(ProductRules.SlugMaxLength); builder.Property(p => p.Sku) .HasMaxLength(ProductRules.SkuMaxLength); builder.Property(p => p.Description) .HasMaxLength(ProductRules.DescriptionMaxLength); builder.Property(p => p.Price) .HasPrecision(18, 2); builder.HasIndex(p => p.Sku) .IsUnique() .HasFilter("[Sku] IS NOT NULL"); builder.HasIndex(p => p.Slug) .IsUnique() .HasFilter("[Slug] IS NOT NULL"); builder.HasOne(p => p.ParentProduct) .WithMany(p => p.Variants) .HasForeignKey(p => p.ParentProductId) .OnDelete(DeleteBehavior.Restrict); builder.HasMany(p => p.Attributes) .WithOne(a => a.Product) .HasForeignKey(a => a.ProductId) .OnDelete(DeleteBehavior.Cascade); builder.HasMany(p => p.Options) .WithOne(o => o.Product) .HasForeignKey(o => o.ProductId) .OnDelete(DeleteBehavior.Cascade); builder.HasMany(p => p.Categories) .WithOne(pc => pc.Product) .HasForeignKey(pc => pc.ProductId) .OnDelete(DeleteBehavior.Cascade); builder.HasMany(p => p.RuleSets) .WithOne(r => r.Product) .HasForeignKey(r => r.ProductId) .OnDelete(DeleteBehavior.Cascade); builder.HasMany(p => p.AxisValues) .WithOne(v => v.ProductVariant) .HasForeignKey(v => v.ProductVariantId) .OnDelete(DeleteBehavior.NoAction); } } public sealed class VariantAxisValueConfig() : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.ToTable("VariantAxisValues", IModuleDb.SchemaName.ToLower()); builder.HasKey(v => new { v.ProductVariantId, v.OptionDefinitionId }); builder.HasOne(v => v.ProductVariant) .WithMany(p => p.AxisValues) .HasForeignKey(v => v.ProductVariantId) .OnDelete(DeleteBehavior.NoAction); builder.HasOne(v => v.OptionDefinition) .WithMany() .HasForeignKey(v => v.OptionDefinitionId) .OnDelete(DeleteBehavior.NoAction); builder.HasOne(v => v.OptionValue) .WithMany() .HasForeignKey(v => v.OptionValueId) .OnDelete(DeleteBehavior.Cascade); } }