diff --git a/University/DatabaseImplement/Implements/DisciplineStorage.cs b/University/DatabaseImplement/Implements/DisciplineStorage.cs index de5a49f..4ff387a 100644 --- a/University/DatabaseImplement/Implements/DisciplineStorage.cs +++ b/University/DatabaseImplement/Implements/DisciplineStorage.cs @@ -3,6 +3,7 @@ using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; using UniversityDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace UniversityDatabaseImplement.Implements { @@ -11,12 +12,15 @@ namespace UniversityDatabaseImplement.Implements public List GetFullList() { using var context = new UniversityDatabase(); - return context.Disciplines.Select(x => x.GetViewModel).ToList(); + return context.Disciplines + .Include(x => x.Statements) + .Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(DisciplineSearchModel model) { using var context = new UniversityDatabase(); return context.Disciplines + .Include(x => x.Statements) .Where(x => ( (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Name) || x.Name.Contains(model.Name)) @@ -33,7 +37,8 @@ namespace UniversityDatabaseImplement.Implements } using var context = new UniversityDatabase(); return context.Disciplines - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + .Include(x => x.Statements) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public DisciplineViewModel? Insert(DisciplineBindingModel model) diff --git a/University/DatabaseImplement/Models/Discipline.cs b/University/DatabaseImplement/Models/Discipline.cs index 6aa5679..3c35ca6 100644 --- a/University/DatabaseImplement/Models/Discipline.cs +++ b/University/DatabaseImplement/Models/Discipline.cs @@ -17,6 +17,21 @@ namespace UniversityDatabaseImplement.Models public virtual List Statements { get; set; } = new(); [ForeignKey("DisciplineId")] public virtual List ReportTypeDisciplines { get; set; } = new(); + private Dictionary? _disciplineStatements; + [NotMapped] + public Dictionary DisciplineStatements + { + get + { + if (_disciplineStatements == null) + { + _disciplineStatements = Statements.ToDictionary( + x => x.Id, x => x as IStatementModel); + } + + return _disciplineStatements; + } + } public static Discipline Create(DisciplineBindingModel model) {