From a5fa00adea5cd4626970d0332e601a770772024f Mon Sep 17 00:00:00 2001 From: GokaPek Date: Mon, 27 May 2024 23:18:20 +0400 Subject: [PATCH] =?UTF-8?q?=D1=85=D0=BE=D1=82=D1=84=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/StatementStorage.cs | 4 ++-- University/UniversityDatabaseImplement/Models/Statement.cs | 4 +++- University/UniversityDatabaseImplement/UniversityDatabase.cs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/University/UniversityDatabaseImplement/Implements/StatementStorage.cs b/University/UniversityDatabaseImplement/Implements/StatementStorage.cs index f3211e7..b888ac9 100644 --- a/University/UniversityDatabaseImplement/Implements/StatementStorage.cs +++ b/University/UniversityDatabaseImplement/Implements/StatementStorage.cs @@ -46,12 +46,12 @@ namespace UniversityDatabaseImplement.Implements public StatementViewModel? Insert(StatementBindingModel model) { - var newStatement = Statement.Create(model); + using var context = new UniversityDatabase(); + var newStatement = Statement.Create(context, model); if (newStatement == null) { return null; } - using var context = new UniversityDatabase(); context.Statements.Add(newStatement); context.SaveChanges(); return context.Statements.Include(x => x.Teacher).FirstOrDefault(x => x.Id == newStatement.Id)?.GetViewModel; diff --git a/University/UniversityDatabaseImplement/Models/Statement.cs b/University/UniversityDatabaseImplement/Models/Statement.cs index 7844172..41dcb98 100644 --- a/University/UniversityDatabaseImplement/Models/Statement.cs +++ b/University/UniversityDatabaseImplement/Models/Statement.cs @@ -23,7 +23,7 @@ namespace UniversityDatabaseImplement.Models public DateTime Date { get; private set; } public virtual User User { get; set; } = new (); public virtual Teacher Teacher { get; set; } = new (); - public static Statement? Create(StatementBindingModel model) + public static Statement? Create(UniversityDatabase context, StatementBindingModel model) { if (model == null) { @@ -33,7 +33,9 @@ namespace UniversityDatabaseImplement.Models { Id = model.Id, UserId = model.UserId, + User = context.Users.First(x => x.Id == model.UserId), TeacherId = model.TeacherId, + Teacher = context.Teachers.First(x => x.Id == model.TeacherId), Name = model.Name, Date = model.Date, }; diff --git a/University/UniversityDatabaseImplement/UniversityDatabase.cs b/University/UniversityDatabaseImplement/UniversityDatabase.cs index 2b73bad..07213d0 100644 --- a/University/UniversityDatabaseImplement/UniversityDatabase.cs +++ b/University/UniversityDatabaseImplement/UniversityDatabase.cs @@ -11,7 +11,7 @@ namespace UniversityDatabaseImplement if (optionsBuilder.IsConfigured == false) { //Возможно понадобится писать вместо (localdb) название пк, вот пк Егора: DESKTOP-N8BRIPR; other-name: LAPTOP-DYCTATOR; other-name: DyCTaTOR - optionsBuilder.UseSqlServer(@"Data Source=DyCTaTOR\SQLEXPRESS;Initial Catalog=UniversityDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-N8BRIPR\SQLEXPRESS;Initial Catalog=UniversityDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); }