using BankContracts.Infrastructure; using BankDatabase.Models; using Microsoft.EntityFrameworkCore; namespace BankDatabase; public class BankDbContext : DbContext { private readonly IConfigurationDatabase? _configurationDatabase; public BankDbContext(IConfigurationDatabase configurationDatabase) { _configurationDatabase = configurationDatabase; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(14, 2)); base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .HasIndex(x => x.Login) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.Email) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.PhoneNumber) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.Name) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.Abbreviation) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.PhoneNumber) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.Email) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.Login) .IsUnique(); modelBuilder.Entity() .HasMany(x => x.Deposits) .WithOne(x => x.Clerk) .HasForeignKey(x => x.ClerkId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasMany(x => x.Clients) .WithOne(x => x.Clerk) .HasForeignKey(x => x.ClerkId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasMany(x => x.Replenishments) .WithOne(x => x.Clerk) .HasForeignKey(x => x.ClerkId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity().HasKey(x => new { x.DepositId, x.CurrencyId }); modelBuilder.Entity().HasKey(x => new { x.ClientId, x.CreditProgramId }); modelBuilder.Entity().HasKey(x => new { x.DepositId, x.ClientId }); modelBuilder.Entity().HasKey(x => new { x.CreditProgramId, x.CurrencyId }); } public DbSet Clerks { get; set; } public DbSet Clients { get; set; } public DbSet CreditPrograms { get; set; } public DbSet Currencies { get; set; } public DbSet Deposits { get; set; } public DbSet Periods { get; set; } public DbSet Replenishments { get; set; } public DbSet Storekeepers { get; set; } public DbSet DepositCurrencies { get; set; } public DbSet CreditProgramClients { get; set; } public DbSet DepositClients { get; set; } public DbSet CurrencyCreditPrograms { get; set; } }