using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WinFormsComponentOrientedHost.Data.Entities; namespace WinFormsComponentOrientedHost.Data; public sealed class AppDbContext : DbContext { private readonly string _cs; public AppDbContext(string connectionString) { _cs = connectionString; } public DbSet Categories => Set(); public DbSet Products => Set(); public DbSet Users => Set(); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseNpgsql(_cs); } } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(b => { b.ToTable("categories", schema: "public"); b.HasKey(x => x.id); b.Property(x => x.name).IsRequired().HasMaxLength(200); b.HasIndex(x => x.name).IsUnique(); }); modelBuilder.Entity(b => { b.ToTable("products", schema: "public"); b.HasKey(x => x.id); b.Property(x => x.name).IsRequired().HasMaxLength(200); b.Property(x => x.categoryText).IsRequired().HasMaxLength(200); b.Property(x => x.description).HasMaxLength(2000); b.Property(x => x.quantity).HasColumnType("numeric(18,3)"); b.HasIndex(x => x.name); }); modelBuilder.Entity(b => { b.ToTable("users", "public"); b.HasKey(x => x.id); b.Property(x => x.login).IsRequired().HasMaxLength(100); b.HasIndex(x => x.login).IsUnique(); b.Property(x => x.passwordHash).IsRequired().HasColumnName("password_hash"); b.Property(x => x.accessLevel).IsRequired().HasColumnName("access_level"); b.Property(x => x.createdAt).HasColumnName("created_at"); }); } }