Этап 3. Исправлена ошибка с циклическим удалением
This commit is contained in:
parent
ee7b714fc6
commit
96a411f449
@ -1,5 +1,12 @@
|
||||
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
|
||||
{
|
||||
@ -13,6 +20,26 @@ namespace HospitalDatabaseImplement
|
||||
}
|
||||
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; }
|
||||
|
@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace HospitalDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(HospitalDatabase))]
|
||||
[Migration("20230405144953_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
[Migration("20230406091825_InitCreate")]
|
||||
partial class InitCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -98,6 +98,7 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Surname")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("TreatmentId")
|
||||
@ -296,9 +297,9 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Medicine", b =>
|
||||
{
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
|
||||
.WithMany()
|
||||
.WithMany("Medicines")
|
||||
.HasForeignKey("ApothecaryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Apothecary");
|
||||
@ -318,9 +319,9 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Prescription", b =>
|
||||
{
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
|
||||
.WithMany()
|
||||
.WithMany("Prescriptions")
|
||||
.HasForeignKey("ApothecaryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Medicine", "Medicine")
|
||||
@ -356,9 +357,9 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Recipe", b =>
|
||||
{
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
|
||||
.WithMany()
|
||||
.WithMany("Recipes")
|
||||
.HasForeignKey("ApothecaryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Apothecary");
|
||||
@ -421,6 +422,15 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
b.Navigation("Treatment");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Apothecary", b =>
|
||||
{
|
||||
b.Navigation("Medicines");
|
||||
|
||||
b.Navigation("Prescriptions");
|
||||
|
||||
b.Navigation("Recipes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Medicine", b =>
|
||||
{
|
||||
b.Navigation("Prescriptions");
|
@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
namespace HospitalDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
public partial class InitCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
@ -60,7 +60,7 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
Name = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
|
||||
Cost = table.Column<double>(type: "float", nullable: false),
|
||||
Dose = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
|
||||
ApothecaryId = table.Column<int>(type: "int", nullable: false, defaultValue: -1)
|
||||
ApothecaryId = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
@ -70,7 +70,7 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
column: x => x.ApothecaryId,
|
||||
principalTable: "Apothecaries",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.SetDefault);
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
@ -91,7 +91,7 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
column: x => x.ApothecaryId,
|
||||
principalTable: "Apothecaries",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
@ -100,7 +100,7 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
Surname = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
Surname = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Patronymic = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
BirthDate = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
@ -162,7 +162,7 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
column: x => x.ApothecaryId,
|
||||
principalTable: "Apothecaries",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
table.ForeignKey(
|
||||
name: "FK_Prescriptions_Medicines_MedicineId",
|
||||
column: x => x.MedicineId,
|
@ -95,6 +95,7 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Surname")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("TreatmentId")
|
||||
@ -293,9 +294,9 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Medicine", b =>
|
||||
{
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
|
||||
.WithMany()
|
||||
.WithMany("Medicines")
|
||||
.HasForeignKey("ApothecaryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Apothecary");
|
||||
@ -315,9 +316,9 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Prescription", b =>
|
||||
{
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
|
||||
.WithMany()
|
||||
.WithMany("Prescriptions")
|
||||
.HasForeignKey("ApothecaryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Medicine", "Medicine")
|
||||
@ -353,9 +354,9 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Recipe", b =>
|
||||
{
|
||||
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
|
||||
.WithMany()
|
||||
.WithMany("Recipes")
|
||||
.HasForeignKey("ApothecaryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Apothecary");
|
||||
@ -418,6 +419,15 @@ namespace HospitalDatabaseImplement.Migrations
|
||||
b.Navigation("Treatment");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Apothecary", b =>
|
||||
{
|
||||
b.Navigation("Medicines");
|
||||
|
||||
b.Navigation("Prescriptions");
|
||||
|
||||
b.Navigation("Recipes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("HospitalDatabaseImplement.Models.Medicine", b =>
|
||||
{
|
||||
b.Navigation("Prescriptions");
|
||||
|
@ -2,6 +2,7 @@
|
||||
using HospitalContracts.ViewModels;
|
||||
using HospitalDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace HospitalDatabaseImplement.Models
|
||||
{
|
||||
@ -15,6 +16,15 @@ namespace HospitalDatabaseImplement.Models
|
||||
[MaxLength(55)]
|
||||
public string Password { get; private set; } = string.Empty;
|
||||
|
||||
[ForeignKey("ApothecaryId")]
|
||||
public virtual List<Medicine> Medicines { get; set; } = new();
|
||||
|
||||
[ForeignKey("ApothecaryId")]
|
||||
public virtual List<Recipe> Recipes { get; set; } = new();
|
||||
|
||||
[ForeignKey("ApothecaryId")]
|
||||
public virtual List<Prescription> Prescriptions { get; set; } = new();
|
||||
|
||||
public static Apothecary? Create(ApothecaryBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
|
@ -22,7 +22,7 @@ namespace HospitalDatabaseImplement.Models
|
||||
public string Dose { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public int ApothecaryId { get; private set; } = -1;
|
||||
public int ApothecaryId { get; private set; }
|
||||
|
||||
public virtual Apothecary Apothecary { get; set; }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user