Files
PIBD24_BoikoM.S._Candyhouse/CandyHouseSolution/CandyHouseDataBase/CandyHouseDataBase.cs
2025-03-13 13:38:00 +04:00

61 lines
2.1 KiB
C#

using CandyHouseBase.Infrastructure;
using CandyHouseDataBase.Models;
using Microsoft.EntityFrameworkCore;
namespace CandyHouseDataBase;
public class CandyHouseDbContext(IConfigurationDatabase configurationDatabase) : DbContext
{
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString);
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Uniqueness for Ingredient
modelBuilder.Entity<Ingredient>()
.HasIndex(x => x.Name)
.IsUnique();
// Uniqueness for Product (only non-deleted)
modelBuilder.Entity<Product>()
.HasIndex(x => new { x.Name, x.IsDeleted })
.IsUnique()
.HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE");
// Composite key for Recipe
modelBuilder.Entity<Recipe>()
.HasKey(x => new { x.ProductId, x.IngredientId });
// Uniqueness for Position (Title for active records)
modelBuilder.Entity<Position>()
.HasIndex(x => new { x.Title, x.IsActual })
.IsUnique()
.HasFilter($"\"{nameof(Position.IsActual)}\" = TRUE");
modelBuilder.Entity<Position>()
.HasIndex(x => new { x.PositionId, x.IsActual })
.IsUnique()
.HasFilter($"\"{nameof(Position.IsActual)}\" = TRUE");
// Uniqueness for Order
modelBuilder.Entity<Order>()
.HasIndex(x => new { x.PekarId, x.OrderDate })
.IsUnique();
}
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Recipe> Recipes { get; set; }
public DbSet<Position> Positions { get; set; }
public DbSet<Pekar> Pekars { get; set; }
public DbSet<PekarHistory> PekarHistories { get; set; }
public DbSet<Salary> Salaries { get; set; }
public DbSet<Order> Orders { get; set; }
}