Files
ShopSolution-backend/Webshop.Infrastructure/Data/ApplicationDbContext.cs
Tizian.Breuch 55eeaca7f4 naming
2025-08-01 15:11:42 +02:00

138 lines
6.0 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!;
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<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);
}
}
}