using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Azure.Core; using Azure; using CarShowroomDatabaseStorage.Entities; using Microsoft.EntityFrameworkCore; namespace CarShowroomDatabaseStorage { public class CarShowroomDatabase : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (optionsBuilder.IsConfigured == false) { optionsBuilder.UseNpgsql("Host=localhost;Port=5433;Database=CarShowroomDatabase;Username=postgres;Password=postgres"); //ptionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=CarShowroomDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { //composite primary keys modelBuilder.Entity().HasKey(sc => new { sc.SaleId, sc.CarId }); modelBuilder.Entity().HasKey(sc => new { sc.SaleId, sc.ServiceId }); //default values modelBuilder.Entity().Property(c => c.Name).HasDefaultValue("empty_string"); modelBuilder.Entity().Property(e => e.Name).HasDefaultValue("empty_string"); modelBuilder.Entity().Property(e => e.Password).HasDefaultValue("empty_string"); modelBuilder.Entity().Property(c => c.Color).HasDefaultValue("empty_string"); modelBuilder.Entity().Property(c => c.Cost).HasDefaultValue(-1); modelBuilder.Entity().Property(c => c.Price).HasDefaultValue(-1); modelBuilder.Entity().Property(c => c.Cost).HasDefaultValue(-1); //on delete actions modelBuilder.Entity() .HasOne(m => m.Make) .WithMany(m => m.Models) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(m => m.Model) .WithMany(m => m.Cars) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(m => m.Client) .WithMany(m => m.Sales) .OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity() .HasOne(m => m.Employee) .WithMany(m => m.Sales) .OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity() .HasOne(m => m.Sale) .WithMany(m => m.SaleServices) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(m => m.Sale) .WithMany(m => m.SaleCars) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(m => m.Service) .WithMany(m => m.SaleServices) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasOne(m => m.Car) .WithMany(m => m.SaleCars) .OnDelete(DeleteBehavior.Cascade); base.OnModelCreating(modelBuilder); } public virtual DbSet Makes { set; get; } public virtual DbSet Models { set; get; } public virtual DbSet Cars { set; get; } public virtual DbSet Services { set; get; } public virtual DbSet Employees { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Sales { set; get; } public virtual DbSet SaleCars { set; get; } public virtual DbSet SaleServices { set; get; } } }