64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
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<Category> Categories => Set<Category>();
|
|
public DbSet<Product> Products => Set<Product>();
|
|
public DbSet<User> Users => Set<User>();
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
optionsBuilder.UseNpgsql(_cs);
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<Category>(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<Product>(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<User>(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");
|
|
});
|
|
}
|
|
}
|