SUBD_PIbd-23_ZakharovRA/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs
2024-05-12 21:44:49 +04:00

87 lines
3.9 KiB
C#

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<SaleCar>().HasKey(sc => new { sc.SaleId, sc.CarId });
modelBuilder.Entity<SaleService>().HasKey(sc => new { sc.SaleId, sc.ServiceId });
//default values
modelBuilder.Entity<Client>().Property(c => c.Name).HasDefaultValue("empty_string");
modelBuilder.Entity<Employee>().Property(e => e.Name).HasDefaultValue("empty_string");
modelBuilder.Entity<Employee>().Property(e => e.Password).HasDefaultValue("empty_string");
modelBuilder.Entity<Car>().Property(c => c.Color).HasDefaultValue("empty_string");
modelBuilder.Entity<Car>().Property(c => c.IsSaled).HasDefaultValue(false);
modelBuilder.Entity<Service>().Property(c => c.Cost).HasDefaultValue(-1);
modelBuilder.Entity<Model>().Property(c => c.Price).HasDefaultValue(-1);
modelBuilder.Entity<Sale>().Property(c => c.Cost).HasDefaultValue(-1);
//on delete actions
modelBuilder.Entity<Model>()
.HasOne(m => m.Make)
.WithMany(m => m.Models)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Car>()
.HasOne(m => m.Model)
.WithMany(m => m.Cars)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Sale>()
.HasOne(m => m.Client)
.WithMany(m => m.Sales)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<Sale>()
.HasOne(m => m.Employee)
.WithMany(m => m.Sales)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<SaleService>()
.HasOne(m => m.Sale)
.WithMany(m => m.SaleServices)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<SaleCar>()
.HasOne(m => m.Sale)
.WithMany(m => m.SaleCars)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<SaleService>()
.HasOne(m => m.Service)
.WithMany(m => m.SaleServices)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<SaleCar>()
.HasOne(m => m.Car)
.WithMany(m => m.SaleCars)
.OnDelete(DeleteBehavior.Cascade);
base.OnModelCreating(modelBuilder);
}
public virtual DbSet<Make> Makes { set; get; }
public virtual DbSet<Model> Models { set; get; }
public virtual DbSet<Car> Cars { set; get; }
public virtual DbSet<Service> Services { set; get; }
public virtual DbSet<Employee> Employees { set; get; }
public virtual DbSet<Client> Clients { set; get; }
public virtual DbSet<Sale> Sales { set; get; }
public virtual DbSet<SaleCar> SaleCars { set; get; }
public virtual DbSet<SaleService> SaleServices { set; get; }
}
}