Files
ShopSolution-backend/Webshop.Infrastructure/Data/ApplicationDbContext.cs
Tizian.Breuch 88a81aa795 beziehuing
2025-09-08 11:28:05 +02:00

192 lines
8.1 KiB
C#

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<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Product> Products { get; set; } = default!;
public DbSet<ProductVariant> ProductVariants { get; set; } = default!;
public DbSet<Categorie> categories { get; set; } = default!;
public DbSet<Customer> Customers { get; set; } = default!;
public DbSet<Address> Addresses { get; set; } = default!;
public DbSet<Order> Orders { get; set; } = default!;
public DbSet<OrderItem> OrderItems { get; set; } = default!;
public DbSet<Review> Reviews { get; set; } = default!;
public DbSet<Discount> Discounts { get; set; } = default!;
public DbSet<Supplier> Suppliers { get; set; } = default!;
public DbSet<ShippingMethod> ShippingMethods { get; set; } = default!;
public DbSet<PaymentMethod> PaymentMethods { get; set; } = default!;
public DbSet<Setting> Settings { get; set; } = default!;
public DbSet<Productcategorie> Productcategories { get; set; } = default!;
public DbSet<ProductDiscount> ProductDiscounts { get; set; } = default!;
public DbSet<CategorieDiscount> categorieDiscounts { get; set; } = default!;
public DbSet<ProductImage> ProductImages { get; set; } = default!;
public DbSet<ShopInfo> ShopInfos { 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<Productcategorie>().HasKey(pc => new { pc.ProductId, pc.categorieId });
modelBuilder.Entity<ProductDiscount>().HasKey(pd => new { pd.ProductId, pd.DiscountId });
modelBuilder.Entity<CategorieDiscount>().HasKey(cd => new { cd.categorieId, cd.DiscountId });
modelBuilder.Entity<Product>().HasIndex(p => p.SKU).IsUnique();
modelBuilder.Entity<Product>().HasIndex(p => p.Slug).IsUnique();
modelBuilder.Entity<Categorie>().HasIndex(c => c.Slug).IsUnique();
modelBuilder.Entity<Discount>().HasIndex(d => d.CouponCode).IsUnique().HasFilter("\"CouponCode\" IS NOT NULL");
modelBuilder.Entity<Setting>().HasIndex(s => s.Key).IsUnique();
modelBuilder.Entity<Order>().HasIndex(o => o.OrderNumber).IsUnique();
modelBuilder.Entity<Product>(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<ProductImage>(entity =>
{
entity.ToTable("ProductImages");
entity.HasOne(pi => pi.Product)
.WithMany(p => p.Images)
.HasForeignKey(pi => pi.ProductId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<ProductVariant>()
.Property(pv => pv.PriceAdjustment).HasPrecision(18, 2);
modelBuilder.Entity<Order>(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<OrderItem>(e =>
{
e.Property(oi => oi.UnitPrice).HasPrecision(18, 2);
e.Property(oi => oi.TotalPrice).HasPrecision(18, 2);
});
modelBuilder.Entity<Discount>(e =>
{
e.Property(d => d.DiscountValue).HasPrecision(18, 2);
e.Property(d => d.MinimumOrderAmount).HasPrecision(18, 2);
});
modelBuilder.Entity<ShippingMethod>(e =>
{
e.Property(sm => sm.BaseCost).HasPrecision(18, 2);
e.Property(sm => sm.MinimumOrderAmount).HasPrecision(18, 2);
});
modelBuilder.Entity<PaymentMethod>()
.Property(pm => pm.ProcessingFee).HasPrecision(18, 2);
modelBuilder.Entity<Categorie>()
.HasOne(c => c.Parentcategorie)
.WithMany(c => c.Subcategories)
.HasForeignKey(c => c.ParentcategorieId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<OrderItem>()
.HasOne(oi => oi.Product)
.WithMany()
.HasForeignKey(oi => oi.ProductId)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<OrderItem>()
.HasOne(oi => oi.ProductVariant)
.WithMany()
.HasForeignKey(oi => oi.ProductVariantId)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<Order>()
.HasOne(o => o.BillingAddress)
.WithMany()
.HasForeignKey(o => o.BillingAddressId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Order>()
.HasOne(o => o.ShippingAddress)
.WithMany()
.HasForeignKey(o => o.ShippingAddressId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ApplicationUser>()
.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<Customer>(c => c.AspNetUserId);
modelBuilder.Entity<ProductDiscount>(entity =>
{
entity.HasOne(pd => pd.Product)
.WithMany(p => p.ProductDiscounts)
.HasForeignKey(pd => pd.ProductId);
entity.HasOne(pd => pd.Discount)
.WithMany(d => d.ProductDiscounts)
.HasForeignKey(pd => pd.DiscountId);
});
// << NEU: Beziehungskonfiguration für CategorieDiscount >>
modelBuilder.Entity<CategorieDiscount>(entity =>
{
entity.HasOne(cd => cd.categorie)
.WithMany(c => c.categorieDiscounts)
.HasForeignKey(cd => cd.categorieId);
entity.HasOne(cd => cd.Discount)
.WithMany(d => d.categorieDiscounts)
.HasForeignKey(cd => cd.DiscountId);
});
modelBuilder.Entity<Review>(entity =>
{
// Beziehung zu Product
entity.HasOne(r => r.Product)
.WithMany(p => p.Reviews)
.HasForeignKey(r => r.ProductId)
.OnDelete(DeleteBehavior.Cascade); // Lösche Bewertungen, wenn das Produkt gelöscht wird
// Beziehung zu Customer
entity.HasOne(r => r.Customer)
.WithMany(c => c.Reviews)
.HasForeignKey(r => r.CustomerId)
.OnDelete(DeleteBehavior.SetNull); // Setze CustomerId auf NULL, wenn der Kunde gelöscht wird
});
}
}
}