using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Webshop.Domain.Entities; using Webshop.Domain.Identity; namespace Webshop.Infrastructure.Data { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public DbSet Products { get; set; } = default!; public DbSet ProductVariants { get; set; } = default!; public DbSet Categories { get; set; } = default!; public DbSet Customers { get; set; } = default!; public DbSet
Addresses { get; set; } = default!; public DbSet Orders { get; set; } = default!; public DbSet OrderItems { get; set; } = default!; public DbSet Reviews { get; set; } = default!; public DbSet Discounts { get; set; } = default!; public DbSet Suppliers { get; set; } = default!; public DbSet ShippingMethods { get; set; } = default!; public DbSet PaymentMethods { get; set; } = default!; public DbSet Settings { get; set; } = default!; public DbSet ProductCategories { get; set; } = default!; public DbSet ProductDiscounts { get; set; } = default!; public DbSet CategoryDiscounts { get; set; } = default!; protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); foreach (var entity in modelBuilder.Model.GetEntityTypes()) { var tableName = entity.GetTableName(); if (tableName != null && tableName.StartsWith("AspNet")) { entity.SetTableName(tableName.Substring(6)); } } modelBuilder.Entity().HasKey(pc => new { pc.ProductId, pc.CategoryId }); modelBuilder.Entity().HasKey(pd => new { pd.ProductId, pd.DiscountId }); modelBuilder.Entity().HasKey(cd => new { cd.CategoryId, cd.DiscountId }); modelBuilder.Entity().HasIndex(p => p.SKU).IsUnique(); modelBuilder.Entity().HasIndex(p => p.Slug).IsUnique(); modelBuilder.Entity().HasIndex(c => c.Slug).IsUnique(); modelBuilder.Entity().HasIndex(d => d.CouponCode).IsUnique().HasFilter("\"CouponCode\" IS NOT NULL"); modelBuilder.Entity().HasIndex(s => s.Key).IsUnique(); modelBuilder.Entity().HasIndex(o => o.OrderNumber).IsUnique(); modelBuilder.Entity(e => { e.Property(p => p.Price).HasPrecision(18, 2); e.Property(p => p.OldPrice).HasPrecision(18, 2); e.Property(p => p.PurchasePrice).HasPrecision(18, 2); e.Property(p => p.Weight).HasPrecision(18, 3); e.Property(p => p.Width).HasPrecision(18, 2); e.Property(p => p.Height).HasPrecision(18, 2); e.Property(p => p.Length).HasPrecision(18, 2); }); modelBuilder.Entity() .Property(pv => pv.PriceAdjustment).HasPrecision(18, 2); modelBuilder.Entity(e => { e.Property(o => o.OrderTotal).HasPrecision(18, 2); e.Property(o => o.ShippingCost).HasPrecision(18, 2); e.Property(o => o.TaxAmount).HasPrecision(18, 2); e.Property(o => o.DiscountAmount).HasPrecision(18, 2); }); modelBuilder.Entity(e => { e.Property(oi => oi.UnitPrice).HasPrecision(18, 2); e.Property(oi => oi.TotalPrice).HasPrecision(18, 2); }); modelBuilder.Entity(e => { e.Property(d => d.DiscountValue).HasPrecision(18, 2); e.Property(d => d.MinimumOrderAmount).HasPrecision(18, 2); }); modelBuilder.Entity(e => { e.Property(sm => sm.BaseCost).HasPrecision(18, 2); e.Property(sm => sm.MinimumOrderAmount).HasPrecision(18, 2); }); modelBuilder.Entity() .Property(pm => pm.ProcessingFee).HasPrecision(18, 2); modelBuilder.Entity() .HasOne(c => c.ParentCategory) .WithMany(c => c.SubCategories) .HasForeignKey(c => c.ParentCategoryId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(oi => oi.Product) .WithMany() .HasForeignKey(oi => oi.ProductId) .OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity() .HasOne(oi => oi.ProductVariant) .WithMany() .HasForeignKey(oi => oi.ProductVariantId) .OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity() .HasOne(o => o.BillingAddress) .WithMany() .HasForeignKey(o => o.BillingAddressId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(o => o.ShippingAddress) .WithMany() .HasForeignKey(o => o.ShippingAddressId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(a => a.Customer) // Ein ApplicationUser hat ein optionales Customer-Profil .WithOne(c => c.User) // Ein Customer-Profil ist mit genau einem User verknüpft .HasForeignKey(c => c.AspNetUserId); } } }