migration

This commit is contained in:
Tizian.Breuch
2025-11-26 17:16:47 +01:00
parent 5510ffa836
commit 3a2ea2c307
5 changed files with 70 additions and 12 deletions

View File

@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Webshop.Domain.Entities;
using Webshop.Domain.Identity;
@@ -26,12 +25,16 @@ namespace Webshop.Infrastructure.Data
public DbSet<PaymentMethod> PaymentMethods { get; set; } = default!;
public DbSet<Setting> Settings { get; set; } = default!;
// Verknüpfungstabellen und Details
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!;
// +++ NEU: Warenkorb Tabellen +++
public DbSet<Cart> Carts { get; set; } = default!;
public DbSet<CartItem> CartItems { get; set; } = default!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -48,6 +51,17 @@ namespace Webshop.Infrastructure.Data
}
}
// +++ NEU: Warenkorb Konfiguration +++
modelBuilder.Entity<Cart>(entity =>
{
// Wenn ein Warenkorb gelöscht wird, lösche alle Items darin automatisch
entity.HasMany(c => c.Items)
.WithOne(i => i.Cart)
.HasForeignKey(i => i.CartId)
.OnDelete(DeleteBehavior.Cascade);
});
// Bestehende Konfigurationen (bleiben unverändert)
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 });
@@ -143,8 +157,8 @@ namespace Webshop.Infrastructure.Data
.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
.HasOne(a => a.Customer)
.WithOne(c => c.User)
.HasForeignKey<Customer>(c => c.AspNetUserId);
modelBuilder.Entity<ProductDiscount>(entity =>
@@ -158,8 +172,6 @@ namespace Webshop.Infrastructure.Data
.HasForeignKey(pd => pd.DiscountId);
});
// << NEU: Beziehungskonfiguration für CategorieDiscount >>
modelBuilder.Entity<CategorieDiscount>(entity =>
{
entity.HasOne(cd => cd.categorie)
@@ -173,20 +185,16 @@ namespace Webshop.Infrastructure.Data
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
.OnDelete(DeleteBehavior.Cascade);
// 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
.OnDelete(DeleteBehavior.SetNull);
});
}
}
}