CourseWork_Borschevskaya_A..../Hospital/HospitalDatabaseImplement/HospitalDatabase.cs

56 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Medicine>().HasOne(e => e.Apothecary)
.WithMany(e => e.Medicines)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder
.Entity<Recipe>().HasOne(e => e.Apothecary)
.WithMany(e => e.Recipes)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder
.Entity<Prescription>().HasOne(e => e.Apothecary)
.WithMany(e => e.Prescriptions)
.OnDelete(DeleteBehavior.Restrict);
}
public virtual DbSet<Apothecary> Apothecaries { set; get; }
public virtual DbSet<Recipe> Recipes { set; get; }
public virtual DbSet<Medicine> Medicines { set; get; }
public virtual DbSet<Prescription> Prescriptions { set; get; }
public virtual DbSet<Patient> Patients { set; get; }
public virtual DbSet<Procedure> Procedures { set; get; }
public virtual DbSet<Treatment> Treatments { set; get; }
public virtual DbSet<RecipeMedicine> RecipeMedicines { set; get; }
public virtual DbSet<RecipeTreatment> RecipeTreatments { set; get; }
public virtual DbSet<ProcedureMedicine> ProcedureMedicines { set; get; }
public virtual DbSet<TreatmentProcedure> TreatmentProcedures { set; get; }
}
}