using Microsoft.EntityFrameworkCore; using SampleWebApi.Models; namespace SampleWebApi.Data; public class SampleDbContext : DbContext { public SampleDbContext(DbContextOptions options) : base(options) { } public DbSet Products { get; set; } = null!; public DbSet Orders { get; set; } = null!; public DbSet OrderItems { get; set; } = null!; public DbSet Customers { get; set; } = null!; protected override void OnModelCreating(ModelBuilder modelBuilder) { // Product configuration modelBuilder.Entity(entity => { entity.HasKey(p => p.Id); entity.Property(p => p.Name).IsRequired().HasMaxLength(200); entity.Property(p => p.Category).IsRequired().HasMaxLength(100); entity.Property(p => p.Price).HasPrecision(10, 2); entity.HasIndex(p => p.Category); entity.HasIndex(p => p.Price); }); // Order configuration modelBuilder.Entity(entity => { entity.HasKey(o => o.Id); entity.Property(o => o.CustomerId).IsRequired().HasMaxLength(50); entity.Property(o => o.TotalAmount).HasPrecision(10, 2); entity.HasIndex(o => o.CustomerId); entity.HasIndex(o => o.OrderDate); entity.HasMany(o => o.Items) .WithOne(oi => oi.Order) .HasForeignKey(oi => oi.OrderId); }); // OrderItem configuration modelBuilder.Entity(entity => { entity.HasKey(oi => oi.Id); entity.Property(oi => oi.UnitPrice).HasPrecision(10, 2); entity.Property(oi => oi.TotalPrice).HasPrecision(10, 2); entity.HasIndex(oi => new { oi.OrderId, oi.ProductId }); }); // Customer configuration modelBuilder.Entity(entity => { entity.HasKey(c => c.Id); entity.Property(c => c.Id).HasMaxLength(50); entity.Property(c => c.Name).IsRequired().HasMaxLength(200); entity.Property(c => c.Email).IsRequired().HasMaxLength(200); entity.HasIndex(c => c.Email).IsUnique(); entity.HasMany(c => c.Orders) .WithOne() .HasForeignKey(o => o.CustomerId); }); } }