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); }