64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using CandyHouseContracts.Infrastructure;
|
|
using CandyHouseDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CandyHouseDatabase;
|
|
|
|
public class CandyHouseDbContext(IConfigurationDatabase configurationDatabase) : DbContext
|
|
{
|
|
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(12, 2));
|
|
base.OnConfiguring(optionsBuilder);
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.Entity<Client>().HasIndex(x => x.PhoneNumber).IsUnique();
|
|
|
|
modelBuilder.Entity<Sale>()
|
|
.HasOne(e => e.Client)
|
|
.WithMany(e => e.Sales)
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
modelBuilder.Entity<Product>().HasIndex(x => x.ProductName).IsUnique();
|
|
|
|
modelBuilder.Entity<Post>()
|
|
.HasIndex(e => new { e.PostName, e.IsActual })
|
|
.IsUnique()
|
|
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
|
|
|
modelBuilder.Entity<Post>()
|
|
.HasIndex(e => new { e.PostId, e.IsActual })
|
|
.IsUnique()
|
|
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
|
|
|
modelBuilder.Entity<SaleProduct>().HasKey(x => new { x.SaleId, x.ProductId });
|
|
}
|
|
|
|
public DbSet<Client> Clients { get; set; }
|
|
|
|
public DbSet<Post> Posts { get; set; }
|
|
|
|
public DbSet<Product> Products { get; set; }
|
|
|
|
public DbSet<ProductHistory> ProductHistories { get; set; }
|
|
|
|
public DbSet<Salary> Salaries { get; set; }
|
|
|
|
public DbSet<Sale> Sales { get; set; }
|
|
|
|
public DbSet<SaleProduct> SaleProducts { get; set; }
|
|
|
|
public DbSet<Employee> Employees { get; set; }
|
|
}
|