using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using {{ProjectName}}.Domain.Entities;
namespace {{ProjectName}}.Infrastructure.Persistence.Configurations;
///
/// EF Core configuration for the Order entity.
///
/// Demonstrates real cross-schema foreign keys to two Core whitelist entities:
/// core.auth_Users (User) and core.tenant_TenantOrganisations
/// (TenantOrganisation — the shared organisation directory). The Core tables
/// themselves are owned by CoreDbContext — this extension context
/// references them via SmartStackExtensionDbContext's
/// ExcludeFromMigrations mapping, so the generated migration carries
/// the FK constraint without recreating the Core tables.
///
/// SchemaConstants is the project-local shim
/// ({{ProjectName}}.Infrastructure.Persistence.SchemaConstants), resolved
/// through the enclosing namespace — no using needed.
///
public class OrderConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
// Table in the 'extensions' schema with a domain prefix
builder.ToTable("biz_Orders", SchemaConstants.Extensions);
builder.HasKey(e => e.Id);
builder.Property(e => e.OrderNumber)
.IsRequired()
.HasMaxLength(50);
builder.Property(e => e.Status)
.IsRequired();
builder.Property(e => e.TotalAmount)
.HasPrecision(18, 2);
builder.Property(e => e.OrderDate)
.IsRequired();
builder.Property(e => e.Notes)
.HasMaxLength(1000);
builder.HasIndex(e => e.OrderNumber).IsUnique();
builder.HasIndex(e => e.CustomerUserId);
builder.HasIndex(e => e.OrganisationId);
builder.HasIndex(e => e.Status);
builder.HasIndex(e => e.OrderDate);
// Real cross-schema foreign keys to Core whitelist entities. EF generates
// ALTER TABLE ... ADD CONSTRAINT FK_... REFERENCES [core].[auth_Users] /
// [core].[tenant_TenantOrganisations] in the extensions migration,
// WITHOUT recreating those Core tables.
builder.HasOne(e => e.Customer)
.WithMany()
.HasForeignKey(e => e.CustomerUserId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(e => e.Organisation)
.WithMany()
.HasForeignKey(e => e.OrganisationId)
.OnDelete(DeleteBehavior.Restrict);
// Soft-delete query filter (DeletedAt from ExtensionBaseEntity)
builder.HasQueryFilter(e => e.DeletedAt == null);
}
}