diff --git a/Course/DatabaseImplement/Models/Machine.cs b/Course/DatabaseImplement/Models/Machine.cs index b006585..e20974a 100644 --- a/Course/DatabaseImplement/Models/Machine.cs +++ b/Course/DatabaseImplement/Models/Machine.cs @@ -34,10 +34,12 @@ namespace DatabaseImplement.Models return _workerMachines; } } - [ForeignKey("WorkerId")] + [ForeignKey("MachineId")] public virtual List Workers { get; set; } = new(); public virtual Guarantor Guarantor { get; set; } - public static Machine? Create(MachineBindingModel model, FactoryGoWorkDatabase context) + [ForeignKey("MachineId")] + public virtual List Products { get; set; } + public static Machine Create(MachineBindingModel model, FactoryGoWorkDatabase context) { return new Machine() { @@ -69,12 +71,7 @@ namespace DatabaseImplement.Models var machineWorkers = context.WorkerMachines.Where(rec => rec.MachineId == model.Id).ToList(); if (machineWorkers != null && machineWorkers.Count > 0) { - context.WorkerMachines.RemoveRange(machineWorkers.Where(rec => !model.MachineWorker.ContainsKey(rec.WorkerId))); - context.SaveChanges(); - foreach (var upWorker in machineWorkers) - { - model.MachineWorker.Remove(upWorker.WorkerId); - } + context.WorkerMachines.RemoveRange(machineWorkers.Where(rec => model.MachineWorker.ContainsKey(rec.WorkerId))); context.SaveChanges(); } var machine = context.Machines.First(x => x.Id == model.Id); diff --git a/Course/DatabaseImplement/Models/Workshop.cs b/Course/DatabaseImplement/Models/Workshop.cs index ef24b57..ad0d499 100644 --- a/Course/DatabaseImplement/Models/Workshop.cs +++ b/Course/DatabaseImplement/Models/Workshop.cs @@ -17,7 +17,6 @@ namespace DatabaseImplement.Models public string Director { get; set; } = string.Empty; [Required] public int UserId { get; set; } - public virtual Guarantor User { get; set; } public int? ProductionId { get; set; } public virtual Production? Production { get; set; } private Dictionary? _workerWorkshops = null; @@ -38,7 +37,11 @@ namespace DatabaseImplement.Models public virtual Guarantor Guarantor { get; set; } public static Workshop? Create(FactoryGoWorkDatabase context, WorkshopBindingModel model) { - return new Workshop() + if(model == null) + { + return null; + } + return new Workshop { Id = model.Id, Title = model.Title, @@ -53,11 +56,26 @@ namespace DatabaseImplement.Models }).ToList(), }; } + public static Workshop Create(WorkshopViewModel model) + { + return new Workshop + { + Id = model.Id, + Title = model.Title, + Address = model.Address, + Director = model.Director, + ProductionId = model.ProductionId, + UserId = model.UserId, + }; + } public void Update(WorkshopBindingModel model) { + if (model == null) + return; Title = model.Title; Address = model.Address; Director = model.Director; + ProductionId = model.ProductionId; } public WorkshopViewModel GetViewModel => new() { @@ -78,11 +96,14 @@ namespace DatabaseImplement.Models context.SaveChanges(); foreach (var upWorker in workshopWorkers) { - model.WorkshopWorker.Remove(upWorker.WorkerId); - } + if (model.WorkshopWorker.ContainsKey(upWorker.WorkerId)) + { + model.WorkshopWorker.Remove(upWorker.WorkerId); + } + } context.SaveChanges(); } - var workshop = context.Workshops.First(x => x.Id == model.Id); + var workshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id); foreach (var dp in model.WorkshopWorker) { context.WorkerWorkshops.Add(new WorkerWorkshop