Init
This commit is contained in:
98
Prefab.Catalog/Data/Configs/ProductConfig.cs
Normal file
98
Prefab.Catalog/Data/Configs/ProductConfig.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
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<Product>("Products", IModuleDb.SchemaName.ToLower())
|
||||
{
|
||||
public override void Configure(EntityTypeBuilder<Product> 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<VariantAxisValue>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<VariantAxisValue> 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user