using ECommerce.Model.Entities; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace ECommerce.Model.Data; public class AppDbContext : IdentityDbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Products => Set(); public DbSet Categories => Set(); public DbSet Customers => Set(); public DbSet Carts => Set(); public DbSet CartItems => Set(); public DbSet Orders => Set(); public DbSet OrderItems => Set(); public DbSet Payments => Set(); public DbSet Reviews => Set(); protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity(e => { e.HasOne(p => p.Category).WithMany(c => c.Products).HasForeignKey(p => p.CategoryId); e.Property(p => p.Price).HasPrecision(18, 2); }); builder.Entity(e => { e.HasOne(c => c.Customer).WithOne(c => c.Cart).HasForeignKey(c => c.CustomerId); e.HasMany(c => c.Items).WithOne(i => i.Cart).HasForeignKey(i => i.CartId); }); builder.Entity(e => e.HasOne(i => i.Product).WithMany().HasForeignKey(i => i.ProductId)); builder.Entity(e => { e.HasOne(o => o.Customer).WithMany(c => c.Orders).HasForeignKey(o => o.CustomerId); e.HasMany(o => o.Items).WithOne(i => i.Order).HasForeignKey(i => i.OrderId); e.HasOne(o => o.Payment).WithOne(p => p.Order).HasForeignKey(p => p.OrderId); e.Property(o => o.TotalAmount).HasPrecision(18, 2); e.Property(o => o.Status).HasConversion(); e.OwnsOne(o => o.ShippingAddress); }); builder.Entity(e => { e.HasOne(i => i.Product).WithMany().HasForeignKey(i => i.ProductId); e.Property(i => i.UnitPrice).HasPrecision(18, 2); }); builder.Entity(e => { e.Property(p => p.Amount).HasPrecision(18, 2); e.Property(p => p.Status).HasConversion(); }); builder.Entity(e => { e.HasOne(r => r.Product).WithMany().HasForeignKey(r => r.ProductId); e.Property(r => r.UserName).HasMaxLength(100); e.Property(r => r.Comment).HasMaxLength(1000); }); builder.Entity(e => e.OwnsOne(c => c.DefaultAddress)); } }