using HospitalDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; using System.Reflection.Metadata; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HospitalDatabaseImplement { public class HospitalDatabase : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (optionsBuilder.IsConfigured == false) { optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=HospitalDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // решение проблемы с циклическим удалением - запрет на удаление аптекарей, если они связаны с другими сущностями modelBuilder .Entity().HasOne(e => e.Apothecary) .WithMany(e => e.Medicines) .OnDelete(DeleteBehavior.Restrict); modelBuilder .Entity().HasOne(e => e.Apothecary) .WithMany(e => e.Recipes) .OnDelete(DeleteBehavior.Restrict); modelBuilder .Entity().HasOne(e => e.Apothecary) .WithMany(e => e.Prescriptions) .OnDelete(DeleteBehavior.Restrict); } public virtual DbSet Apothecaries { set; get; } public virtual DbSet Recipes { set; get; } public virtual DbSet Medicines { set; get; } public virtual DbSet Prescriptions { set; get; } public virtual DbSet Patients { set; get; } public virtual DbSet Procedures { set; get; } public virtual DbSet Treatments { set; get; } public virtual DbSet RecipeMedicines { set; get; } public virtual DbSet RecipeTreatments { set; get; } public virtual DbSet ProcedureMedicines { set; get; } public virtual DbSet TreatmentProcedures { set; get; } } }